Spaces:
Sleeping
Sleeping
Fazeel Asghar
commited on
Commit
·
c9ff9b4
1
Parent(s):
9ce3a35
Changes in Interface
Browse files
app.py
CHANGED
|
@@ -21,7 +21,7 @@ class ChatInput(BaseModel):
|
|
| 21 |
session_id: str
|
| 22 |
input: str
|
| 23 |
|
| 24 |
-
def chat_logic(session_id: str, user_input: str)
|
| 25 |
if session_id not in session_memory_dict:
|
| 26 |
session_memory_dict[session_id] = []
|
| 27 |
|
|
@@ -48,14 +48,49 @@ def chat_logic(session_id: str, user_input: str) -> str:
|
|
| 48 |
print(f"[Session: {session_id}] AI Response: {ai_response}")
|
| 49 |
return ai_response
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
def
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
session_id: str
|
| 22 |
input: str
|
| 23 |
|
| 24 |
+
def chat_logic(session_id: str, user_input: str):
|
| 25 |
if session_id not in session_memory_dict:
|
| 26 |
session_memory_dict[session_id] = []
|
| 27 |
|
|
|
|
| 48 |
print(f"[Session: {session_id}] AI Response: {ai_response}")
|
| 49 |
return ai_response
|
| 50 |
|
| 51 |
+
# UI logic for message display
|
| 52 |
+
def chat_interface(user_input, chat_history, session_id):
|
| 53 |
+
ai_reply = chat_logic(session_id, user_input)
|
| 54 |
+
chat_history.append((user_input, ai_reply))
|
| 55 |
+
return chat_history, ""
|
| 56 |
+
|
| 57 |
+
# Clear session memory
|
| 58 |
+
def clear_session(session_id):
|
| 59 |
+
session_memory_dict.pop(session_id, None)
|
| 60 |
+
return [], "gradio_default"
|
| 61 |
+
|
| 62 |
+
with gr.Blocks(css="""
|
| 63 |
+
.chatbot .message.user { background-color: #dcf8c6 !important; color: #000; }
|
| 64 |
+
.chatbot .message.bot { background-color: #ececec !important; color: #000; }
|
| 65 |
+
""") as demo:
|
| 66 |
+
gr.Markdown("### 🤖 WhatsApp-style Chatbot powered by Groq LLaMA3")
|
| 67 |
+
|
| 68 |
+
with gr.Row():
|
| 69 |
+
session_id = gr.Textbox(label="Session ID", value="gradio_default", interactive=True)
|
| 70 |
+
clear_btn = gr.Button("🔄 Clear Session")
|
| 71 |
+
|
| 72 |
+
chatbot = gr.Chatbot(label="Chat with AI", elem_classes="chatbot")
|
| 73 |
+
user_input = gr.Textbox(label="Type your message...", placeholder="Message...", lines=1)
|
| 74 |
+
send_btn = gr.Button("Send", variant="primary")
|
| 75 |
+
|
| 76 |
+
state = gr.State([])
|
| 77 |
+
|
| 78 |
+
send_btn.click(
|
| 79 |
+
fn=chat_interface,
|
| 80 |
+
inputs=[user_input, state, session_id],
|
| 81 |
+
outputs=[chatbot, user_input]
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
user_input.submit(
|
| 85 |
+
fn=chat_interface,
|
| 86 |
+
inputs=[user_input, state, session_id],
|
| 87 |
+
outputs=[chatbot, user_input]
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
clear_btn.click(
|
| 91 |
+
fn=clear_session,
|
| 92 |
+
inputs=[session_id],
|
| 93 |
+
outputs=[chatbot, session_id]
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
demo.launch()
|