Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,45 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
class SimpleChatBot:
|
| 5 |
def __init__(self):
|
| 6 |
-
self.
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
def get_response(self, message):
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
chatbot = SimpleChatBot()
|
| 17 |
|
| 18 |
def respond(message, chat_history):
|
| 19 |
-
|
|
|
|
| 20 |
chat_history.append({"role": "user", "content": message})
|
| 21 |
chat_history.append({"role": "assistant", "content": response})
|
| 22 |
return chat_history, ""
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from groq import Groq
|
| 4 |
+
|
| 5 |
+
# Set up the Groq client with the secret key
|
| 6 |
+
groq_key = os.getenv('groq_key')
|
| 7 |
+
client = Groq(api_key=groq_key)
|
| 8 |
|
| 9 |
class SimpleChatBot:
|
| 10 |
def __init__(self):
|
| 11 |
+
self.initial_prompt = [
|
| 12 |
+
{
|
| 13 |
+
"role": "system",
|
| 14 |
+
"content": "你是一個老師"
|
| 15 |
+
}
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
def get_response(self, message, chat_history):
|
| 19 |
+
messages = self.initial_prompt + chat_history
|
| 20 |
+
messages.append({"role": "user", "content": message})
|
| 21 |
+
|
| 22 |
+
completion = client.chat.completions.create(
|
| 23 |
+
model="llama3-8b-8192",
|
| 24 |
+
messages=messages,
|
| 25 |
+
temperature=1,
|
| 26 |
+
max_tokens=1024,
|
| 27 |
+
top_p=1,
|
| 28 |
+
stream=True,
|
| 29 |
+
stop=None,
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
response_content = ""
|
| 33 |
+
for chunk in completion:
|
| 34 |
+
response_content += chunk.choices[0].delta.content or ""
|
| 35 |
+
|
| 36 |
+
return response_content
|
| 37 |
|
| 38 |
chatbot = SimpleChatBot()
|
| 39 |
|
| 40 |
def respond(message, chat_history):
|
| 41 |
+
chat_history = [{"role": entry["role"], "content": entry["content"]} for entry in chat_history]
|
| 42 |
+
response = chatbot.get_response(message, chat_history)
|
| 43 |
chat_history.append({"role": "user", "content": message})
|
| 44 |
chat_history.append({"role": "assistant", "content": response})
|
| 45 |
return chat_history, ""
|