Spaces:
Sleeping
Sleeping
Fazeel Asghar
commited on
Commit
·
79b3d3d
1
Parent(s):
944b664
Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
from groq import Groq
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
load_dotenv()
|
| 7 |
+
|
| 8 |
+
client = Groq()
|
| 9 |
+
session_memory_dict = {}
|
| 10 |
+
|
| 11 |
+
system_prompt = {
|
| 12 |
+
"role": "system",
|
| 13 |
+
"content": (
|
| 14 |
+
"You are a friendly bot who responds kindly, even if the user is frustrated or angry. "
|
| 15 |
+
"You are very knowledgeable and cute. "
|
| 16 |
+
"Maintain conversation in a respectful and helpful manner."
|
| 17 |
+
)
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
class ChatInput(BaseModel):
|
| 21 |
+
session_id: str
|
| 22 |
+
input: str
|
| 23 |
+
|
| 24 |
+
def chat_logic(session_id: str, user_input: str) -> str:
|
| 25 |
+
if session_id not in session_memory_dict:
|
| 26 |
+
session_memory_dict[session_id] = []
|
| 27 |
+
|
| 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 |
+
"role": "user",
|
| 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 |
+
ai_response = completion.choices[0].message.content
|
| 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 |
+
# Gradio Interface
|
| 52 |
+
def gradio_chat(user_input, session_id="gradio_default"):
|
| 53 |
+
return chat_logic(session_id=session_id, user_input=user_input)
|
| 54 |
+
|
| 55 |
+
gr.Interface(
|
| 56 |
+
fn=gradio_chat,
|
| 57 |
+
inputs=[gr.Textbox(label="Your message"), gr.Textbox(label="Session ID", value="gradio_default")],
|
| 58 |
+
outputs=gr.Textbox(label="Response"),
|
| 59 |
+
title="Groq ChatBot",
|
| 60 |
+
description="Chat with Groq's LLaMA3 model. Handles sessions separately using IDs."
|
| 61 |
+
).launch()
|