Update app.py
Browse files
app.py
CHANGED
|
@@ -2,135 +2,124 @@ import gradio as gr
|
|
| 2 |
import time
|
| 3 |
|
| 4 |
# ========================================
|
| 5 |
-
# π¬
|
| 6 |
# ========================================
|
| 7 |
def ai_response(message, history):
|
| 8 |
-
#
|
| 9 |
-
time.sleep(
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
# ========================================
|
| 14 |
-
#
|
| 15 |
# ========================================
|
| 16 |
-
with gr.Blocks(
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
background-color: #2a2a2a;
|
| 78 |
-
padding: 20px;
|
| 79 |
-
border-top: 2px solid #444;
|
| 80 |
-
display: flex;
|
| 81 |
-
align-items: center;
|
| 82 |
-
}
|
| 83 |
-
input[type='text'] {
|
| 84 |
-
flex: 1;
|
| 85 |
-
background-color: #444;
|
| 86 |
-
color: #f0f0f0;
|
| 87 |
-
border: none;
|
| 88 |
-
border-radius: 25px;
|
| 89 |
-
padding: 12px 20px;
|
| 90 |
-
font-size: 15px;
|
| 91 |
-
outline: none;
|
| 92 |
-
}
|
| 93 |
-
button.send-btn {
|
| 94 |
-
margin-left: 10px;
|
| 95 |
-
background: linear-gradient(135deg, #03dac6 0%, #00bfa5 100%);
|
| 96 |
-
border: none;
|
| 97 |
-
color: #0d0d0d;
|
| 98 |
-
font-weight: bold;
|
| 99 |
-
border-radius: 25px;
|
| 100 |
-
padding: 12px 20px;
|
| 101 |
-
cursor: pointer;
|
| 102 |
-
transition: 0.3s;
|
| 103 |
-
}
|
| 104 |
-
button.send-btn:hover {
|
| 105 |
-
transform: scale(1.05);
|
| 106 |
-
box-shadow: 0 0 10px rgba(3, 218, 198, 0.5);
|
| 107 |
-
}
|
| 108 |
-
</style>
|
| 109 |
-
""")
|
| 110 |
|
| 111 |
-
#
|
| 112 |
-
with gr.Row(
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
gr.
|
| 119 |
-
gr.Markdown("
|
|
|
|
| 120 |
|
| 121 |
# Main Chat Section
|
| 122 |
-
with gr.Column(
|
| 123 |
-
chatbot = gr.Chatbot(
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
def respond(message, history):
|
| 128 |
-
history = history or []
|
| 129 |
-
reply = ai_response(message, history)
|
| 130 |
-
history.append((message, reply))
|
| 131 |
-
return "", history
|
| 132 |
|
|
|
|
| 133 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 134 |
send.click(respond, [msg, chatbot], [msg, chatbot])
|
| 135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
demo.launch()
|
|
|
|
| 2 |
import time
|
| 3 |
|
| 4 |
# ========================================
|
| 5 |
+
# π¬ AI Response Function (Replace this with your real AI)
|
| 6 |
# ========================================
|
| 7 |
def ai_response(message, history):
|
| 8 |
+
# Example: Simulate AI typing delay
|
| 9 |
+
time.sleep(0.8)
|
| 10 |
+
# You can connect this to any API (e.g., DeepSeek, Hugging Face, OpenAI)
|
| 11 |
+
return f"π€ AI: I understood your question β '{message}'. Letβs explore that together!"
|
| 12 |
+
|
| 13 |
+
# ========================================
|
| 14 |
+
# βοΈ Chat Logic
|
| 15 |
+
# ========================================
|
| 16 |
+
def respond(message, history):
|
| 17 |
+
history = history or []
|
| 18 |
+
bot_message = ai_response(message, history)
|
| 19 |
+
history.append((message, bot_message))
|
| 20 |
+
return "", history
|
| 21 |
|
| 22 |
|
| 23 |
# ========================================
|
| 24 |
+
# π¨ Gradio Interface
|
| 25 |
# ========================================
|
| 26 |
+
with gr.Blocks(css="""
|
| 27 |
+
body {
|
| 28 |
+
background: linear-gradient(135deg, #0d0d0d 0%, #1a1a1a 100%);
|
| 29 |
+
color: #f0f0f0;
|
| 30 |
+
font-family: 'Roboto', sans-serif;
|
| 31 |
+
}
|
| 32 |
+
#chatbot {
|
| 33 |
+
background-color: transparent !important;
|
| 34 |
+
border: none !important;
|
| 35 |
+
}
|
| 36 |
+
.send-btn {
|
| 37 |
+
background: linear-gradient(135deg, #03dac6 0%, #00bfa5 100%) !important;
|
| 38 |
+
color: #0d0d0d !important;
|
| 39 |
+
border: none !important;
|
| 40 |
+
font-weight: bold;
|
| 41 |
+
border-radius: 25px !important;
|
| 42 |
+
padding: 10px 20px !important;
|
| 43 |
+
transition: 0.3s;
|
| 44 |
+
}
|
| 45 |
+
.send-btn:hover {
|
| 46 |
+
transform: scale(1.05);
|
| 47 |
+
box-shadow: 0 0 12px rgba(3, 218, 198, 0.6);
|
| 48 |
+
}
|
| 49 |
+
.sidebar {
|
| 50 |
+
background-color: #2a2a2a;
|
| 51 |
+
padding: 25px;
|
| 52 |
+
border-right: 2px solid #444;
|
| 53 |
+
box-shadow: inset -5px 0 10px rgba(0,0,0,0.4);
|
| 54 |
+
height: 100vh;
|
| 55 |
+
}
|
| 56 |
+
.sidebar h2 {
|
| 57 |
+
color: #03dac6;
|
| 58 |
+
text-align: center;
|
| 59 |
+
font-weight: 400;
|
| 60 |
+
margin-bottom: 25px;
|
| 61 |
+
}
|
| 62 |
+
.sidebar-btn {
|
| 63 |
+
display: block;
|
| 64 |
+
width: 100%;
|
| 65 |
+
background: transparent;
|
| 66 |
+
color: #f0f0f0;
|
| 67 |
+
border: none;
|
| 68 |
+
text-align: left;
|
| 69 |
+
padding: 12px 15px;
|
| 70 |
+
border-radius: 8px;
|
| 71 |
+
font-size: 15px;
|
| 72 |
+
margin-bottom: 5px;
|
| 73 |
+
cursor: pointer;
|
| 74 |
+
transition: all 0.3s ease;
|
| 75 |
+
}
|
| 76 |
+
.sidebar-btn:hover {
|
| 77 |
+
background-color: #444;
|
| 78 |
+
transform: translateX(5px);
|
| 79 |
+
}
|
| 80 |
+
.footer {
|
| 81 |
+
font-size: 12px;
|
| 82 |
+
text-align: center;
|
| 83 |
+
margin-top: auto;
|
| 84 |
+
color: #aaa;
|
| 85 |
+
}
|
| 86 |
+
""") as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
# ========== Layout ==========
|
| 89 |
+
with gr.Row():
|
| 90 |
+
# Sidebar Section
|
| 91 |
+
with gr.Column(scale=1, elem_classes="sidebar"):
|
| 92 |
+
gr.HTML("<h2>AI Study Agent</h2>")
|
| 93 |
+
tutor_btn = gr.Button("π Study Tutor", elem_classes="sidebar-btn")
|
| 94 |
+
planner_btn = gr.Button("ποΈ Study Planner", elem_classes="sidebar-btn")
|
| 95 |
+
advisor_btn = gr.Button("π§ Study Advisor", elem_classes="sidebar-btn")
|
| 96 |
+
gr.Markdown("<hr>")
|
| 97 |
+
gr.Markdown("β¨ <small>Designed by Cherry</small>", elem_classes="footer")
|
| 98 |
|
| 99 |
# Main Chat Section
|
| 100 |
+
with gr.Column(scale=4):
|
| 101 |
+
chatbot = gr.Chatbot(label="", elem_id="chatbot", height=550)
|
| 102 |
+
with gr.Row():
|
| 103 |
+
msg = gr.Textbox(placeholder="Type your question here...", show_label=False, scale=5)
|
| 104 |
+
send = gr.Button("Send", elem_classes="send-btn", scale=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
# Chat connections
|
| 107 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 108 |
send.click(respond, [msg, chatbot], [msg, chatbot])
|
| 109 |
|
| 110 |
+
# Sidebar button responses
|
| 111 |
+
def sidebar_action(choice, history):
|
| 112 |
+
response = f"β¨ You selected **{choice}** β How can I assist with that?"
|
| 113 |
+
history = history or []
|
| 114 |
+
history.append((f"{choice}", response))
|
| 115 |
+
return history
|
| 116 |
+
|
| 117 |
+
tutor_btn.click(sidebar_action, ["Study Tutor", chatbot], chatbot)
|
| 118 |
+
planner_btn.click(sidebar_action, ["Study Planner", chatbot], chatbot)
|
| 119 |
+
advisor_btn.click(sidebar_action, ["Study Advisor", chatbot], chatbot)
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
# ========================================
|
| 123 |
+
# π Launch App
|
| 124 |
+
# ========================================
|
| 125 |
demo.launch()
|