Update app.py
Browse files
app.py
CHANGED
|
@@ -5,12 +5,12 @@ from groq import Groq
|
|
| 5 |
api_key = os.getenv("GROQ_API_KEY")
|
| 6 |
client = Groq(api_key=api_key)
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def chat_with_bot_stream(user_input):
|
| 11 |
-
global conversation_history
|
| 12 |
conversation_history.append({"role": "user", "content": user_input})
|
| 13 |
-
|
| 14 |
if len(conversation_history) == 1:
|
| 15 |
conversation_history.insert(0, {
|
| 16 |
"role": "system",
|
|
@@ -28,7 +28,7 @@ def chat_with_bot_stream(user_input):
|
|
| 28 |
"Limit your responses to music-related inquiries only. Limit your responses to 10 lines or less."
|
| 29 |
)
|
| 30 |
})
|
| 31 |
-
|
| 32 |
completion = client.chat.completions.create(
|
| 33 |
model="llama-3.3-70b-versatile",
|
| 34 |
messages=conversation_history,
|
|
@@ -37,18 +37,18 @@ def chat_with_bot_stream(user_input):
|
|
| 37 |
top_p=0.9,
|
| 38 |
stream=True,
|
| 39 |
)
|
| 40 |
-
|
| 41 |
response_content = ""
|
| 42 |
for chunk in completion:
|
| 43 |
response_content += chunk.choices[0].delta.content or ""
|
| 44 |
-
|
| 45 |
conversation_history.append({"role": "assistant", "content": response_content})
|
| 46 |
-
|
| 47 |
return [
|
| 48 |
-
(msg["content"] if msg["role"] == "user" else None,
|
| 49 |
msg["content"] if msg["role"] == "assistant" else None)
|
| 50 |
for msg in conversation_history
|
| 51 |
-
]
|
| 52 |
|
| 53 |
# word translation
|
| 54 |
translations = {
|
|
@@ -417,10 +417,12 @@ with gr.Blocks(css=None) as demo:
|
|
| 417 |
elem_id="send_button",
|
| 418 |
scale=1
|
| 419 |
)
|
|
|
|
|
|
|
| 420 |
send_button.click(
|
| 421 |
fn=chat_with_bot_stream,
|
| 422 |
-
inputs=user_input,
|
| 423 |
-
outputs=chatbot
|
| 424 |
).then(
|
| 425 |
fn=lambda: "",
|
| 426 |
inputs=None,
|
|
|
|
| 5 |
api_key = os.getenv("GROQ_API_KEY")
|
| 6 |
client = Groq(api_key=api_key)
|
| 7 |
|
| 8 |
+
def chat_with_bot_stream(user_input, conversation_history):
|
| 9 |
+
if conversation_history is None:
|
| 10 |
+
conversation_history = []
|
| 11 |
|
|
|
|
|
|
|
| 12 |
conversation_history.append({"role": "user", "content": user_input})
|
| 13 |
+
|
| 14 |
if len(conversation_history) == 1:
|
| 15 |
conversation_history.insert(0, {
|
| 16 |
"role": "system",
|
|
|
|
| 28 |
"Limit your responses to music-related inquiries only. Limit your responses to 10 lines or less."
|
| 29 |
)
|
| 30 |
})
|
| 31 |
+
|
| 32 |
completion = client.chat.completions.create(
|
| 33 |
model="llama-3.3-70b-versatile",
|
| 34 |
messages=conversation_history,
|
|
|
|
| 37 |
top_p=0.9,
|
| 38 |
stream=True,
|
| 39 |
)
|
| 40 |
+
|
| 41 |
response_content = ""
|
| 42 |
for chunk in completion:
|
| 43 |
response_content += chunk.choices[0].delta.content or ""
|
| 44 |
+
|
| 45 |
conversation_history.append({"role": "assistant", "content": response_content})
|
| 46 |
+
|
| 47 |
return [
|
| 48 |
+
(msg["content"] if msg["role"] == "user" else None,
|
| 49 |
msg["content"] if msg["role"] == "assistant" else None)
|
| 50 |
for msg in conversation_history
|
| 51 |
+
], conversation_history
|
| 52 |
|
| 53 |
# word translation
|
| 54 |
translations = {
|
|
|
|
| 417 |
elem_id="send_button",
|
| 418 |
scale=1
|
| 419 |
)
|
| 420 |
+
conversation_state = gr.State(value=[])
|
| 421 |
+
|
| 422 |
send_button.click(
|
| 423 |
fn=chat_with_bot_stream,
|
| 424 |
+
inputs=[user_input, conversation_state],
|
| 425 |
+
outputs=[chatbot, conversation_state]
|
| 426 |
).then(
|
| 427 |
fn=lambda: "",
|
| 428 |
inputs=None,
|