Spaces:
Sleeping
Sleeping
Fazeel Asghar
commited on
Commit
·
61230db
1
Parent(s):
9a5fa88
Set up
Browse files
app.py
CHANGED
|
@@ -28,24 +28,14 @@ def chat_logic(session_id: str, user_input: str):
|
|
| 28 |
if not any(m["role"] == "system" for m in session_memory_dict[session_id]):
|
| 29 |
session_memory_dict[session_id].insert(0, system_prompt)
|
| 30 |
|
| 31 |
-
session_memory_dict[session_id].append({
|
| 32 |
-
|
| 33 |
-
"content": user_input
|
| 34 |
-
})
|
| 35 |
-
|
| 36 |
-
completion = client.chat.completions.create(
|
| 37 |
model="llama3-8b-8192",
|
| 38 |
messages=session_memory_dict[session_id]
|
| 39 |
)
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
session_memory_dict[session_id].append({
|
| 44 |
-
"role": "assistant",
|
| 45 |
-
"content": ai_response
|
| 46 |
-
})
|
| 47 |
-
|
| 48 |
-
print(f"[Session: {session_id}] AI Response: {ai_response}")
|
| 49 |
return ai_response
|
| 50 |
|
| 51 |
def chat_interface(user_input, chat_history, session_id):
|
|
@@ -58,64 +48,64 @@ def clear_session(session_id):
|
|
| 58 |
return [], "gradio_default"
|
| 59 |
|
| 60 |
with gr.Blocks(css="""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
.chatbot .message.user {
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
border-radius: 12px;
|
| 66 |
-
max-width: 75%;
|
| 67 |
-
margin: 6px 0;
|
| 68 |
-
align-self: flex-end;
|
| 69 |
-
font-size: 15px;
|
| 70 |
}
|
| 71 |
.chatbot .message.bot {
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
border-radius: 12px;
|
| 76 |
-
max-width: 75%;
|
| 77 |
-
margin: 6px 0;
|
| 78 |
-
align-self: flex-start;
|
| 79 |
-
font-size: 15px;
|
| 80 |
}
|
| 81 |
-
.
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
| 86 |
}
|
| 87 |
""") as demo:
|
| 88 |
|
| 89 |
-
gr.Markdown("### 🤖 AI Chat Assistant (
|
| 90 |
|
| 91 |
state = gr.State([])
|
| 92 |
-
|
| 93 |
-
with gr.Row():
|
| 94 |
-
user_input = gr.Textbox(
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
session_id = gr.Textbox(label="Session ID", value="gradio_default", interactive=True)
|
| 101 |
clear_btn = gr.Button("Clear Chat")
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
)
|
| 108 |
-
|
| 109 |
-
user_input.submit(
|
| 110 |
-
fn=chat_interface,
|
| 111 |
-
inputs=[user_input, state, session_id],
|
| 112 |
-
outputs=[chatbot, user_input]
|
| 113 |
-
)
|
| 114 |
-
|
| 115 |
-
clear_btn.click(
|
| 116 |
-
fn=clear_session,
|
| 117 |
-
inputs=[session_id],
|
| 118 |
-
outputs=[chatbot, session_id]
|
| 119 |
-
)
|
| 120 |
|
| 121 |
-
demo.launch()
|
|
|
|
| 28 |
if not any(m["role"] == "system" for m in session_memory_dict[session_id]):
|
| 29 |
session_memory_dict[session_id].insert(0, system_prompt)
|
| 30 |
|
| 31 |
+
session_memory_dict[session_id].append({"role": "user", "content": user_input})
|
| 32 |
+
res = client.chat.completions.create(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
model="llama3-8b-8192",
|
| 34 |
messages=session_memory_dict[session_id]
|
| 35 |
)
|
| 36 |
+
ai_response = res.choices[0].message.content
|
| 37 |
+
session_memory_dict[session_id].append({"role": "assistant", "content": ai_response})
|
| 38 |
+
print(f"[{session_id}] AI:", ai_response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
return ai_response
|
| 40 |
|
| 41 |
def chat_interface(user_input, chat_history, session_id):
|
|
|
|
| 48 |
return [], "gradio_default"
|
| 49 |
|
| 50 |
with gr.Blocks(css="""
|
| 51 |
+
.chatbot {
|
| 52 |
+
display: flex;
|
| 53 |
+
flex-direction: column;
|
| 54 |
+
height: 70vh;
|
| 55 |
+
overflow-y: auto;
|
| 56 |
+
padding: 10px;
|
| 57 |
+
}
|
| 58 |
+
.chatbot .message {
|
| 59 |
+
width: fit-content;
|
| 60 |
+
max-width: 70%;
|
| 61 |
+
padding: 12px 16px;
|
| 62 |
+
margin: 6px 0;
|
| 63 |
+
border-radius: 12px;
|
| 64 |
+
line-height: 1.5;
|
| 65 |
+
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
| 66 |
+
}
|
| 67 |
.chatbot .message.user {
|
| 68 |
+
background-color: #0069D9;
|
| 69 |
+
color: #fff;
|
| 70 |
+
align-self: flex-end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
}
|
| 72 |
.chatbot .message.bot {
|
| 73 |
+
background-color: #f8f9fa;
|
| 74 |
+
color: #212529;
|
| 75 |
+
align-self: flex-start;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
}
|
| 77 |
+
.chat-container {
|
| 78 |
+
display: flex; flex-direction: column; height: 100%;
|
| 79 |
+
}
|
| 80 |
+
.input-row, .session-row {
|
| 81 |
+
display: flex; gap: 8px; align-items: center;
|
| 82 |
+
}
|
| 83 |
+
input, button {
|
| 84 |
+
font-size: 1rem;
|
| 85 |
}
|
| 86 |
""") as demo:
|
| 87 |
|
| 88 |
+
gr.Markdown("### 🤖 AI Chat Assistant (Clean & Responsive UI)")
|
| 89 |
|
| 90 |
state = gr.State([])
|
| 91 |
+
# Input at top
|
| 92 |
+
with gr.Row(elem_classes="input-row"):
|
| 93 |
+
user_input = gr.Textbox(
|
| 94 |
+
label="", placeholder="Type your message here...", lines=1, interactive=True
|
| 95 |
+
)
|
| 96 |
+
send_btn = gr.Button("Send", variant="primary")
|
| 97 |
+
|
| 98 |
+
# Chat area
|
| 99 |
+
chatbot = gr.Chatbot(elem_classes="chatbot")
|
| 100 |
+
|
| 101 |
+
# Session controls at bottom
|
| 102 |
+
with gr.Row(elem_classes="session-row"):
|
| 103 |
session_id = gr.Textbox(label="Session ID", value="gradio_default", interactive=True)
|
| 104 |
clear_btn = gr.Button("Clear Chat")
|
| 105 |
|
| 106 |
+
# Interaction wiring
|
| 107 |
+
send_btn.click(fn=chat_interface, inputs=[user_input, state, session_id], outputs=[chatbot, user_input])
|
| 108 |
+
user_input.submit(fn=chat_interface, inputs=[user_input, state, session_id], outputs=[chatbot, user_input])
|
| 109 |
+
clear_btn.click(fn=clear_session, inputs=[session_id], outputs=[chatbot, session_id])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
+
demo.launch()
|