Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,15 @@ from openai import OpenAI
|
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
load_dotenv()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def respond(
|
| 9 |
message,
|
|
@@ -13,16 +22,7 @@ def respond(
|
|
| 13 |
temperature,
|
| 14 |
top_p,
|
| 15 |
):
|
| 16 |
-
|
| 17 |
-
if not api_key:
|
| 18 |
-
raise RuntimeError("GROQ_API_KEY is not set")
|
| 19 |
-
|
| 20 |
-
client = OpenAI(
|
| 21 |
-
api_key=api_key,
|
| 22 |
-
base_url="https://api.groq.com/openai/v1",
|
| 23 |
-
timeout=120.0
|
| 24 |
-
)
|
| 25 |
-
|
| 26 |
def sanitize_messages(history):
|
| 27 |
sanitized = []
|
| 28 |
for m in history:
|
|
@@ -41,20 +41,23 @@ def respond(
|
|
| 41 |
|
| 42 |
response_text = ""
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
|
|
|
|
|
|
|
| 58 |
|
| 59 |
|
| 60 |
chatbot = gr.ChatInterface(
|
|
@@ -74,4 +77,4 @@ chatbot = gr.ChatInterface(
|
|
| 74 |
)
|
| 75 |
|
| 76 |
if __name__ == "__main__":
|
| 77 |
-
chatbot.queue().launch()
|
|
|
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
load_dotenv()
|
| 7 |
+
api_key = os.getenv("GROQ_API_KEY")
|
| 8 |
+
if not api_key:
|
| 9 |
+
raise RuntimeError("GROQ_API_KEY is not set")
|
| 10 |
+
|
| 11 |
+
client = OpenAI(
|
| 12 |
+
api_key=api_key,
|
| 13 |
+
base_url="https://api.groq.com/openai/v1",
|
| 14 |
+
timeout=120.0
|
| 15 |
+
)
|
| 16 |
|
| 17 |
def respond(
|
| 18 |
message,
|
|
|
|
| 22 |
temperature,
|
| 23 |
top_p,
|
| 24 |
):
|
| 25 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
def sanitize_messages(history):
|
| 27 |
sanitized = []
|
| 28 |
for m in history:
|
|
|
|
| 41 |
|
| 42 |
response_text = ""
|
| 43 |
|
| 44 |
+
try:
|
| 45 |
+
stream = client.chat.completions.create(
|
| 46 |
+
model="qwen/qwen3-32b",
|
| 47 |
+
messages=messages,
|
| 48 |
+
max_tokens=max_tokens,
|
| 49 |
+
temperature=temperature,
|
| 50 |
+
top_p=top_p,
|
| 51 |
+
stream=True,
|
| 52 |
+
)
|
| 53 |
|
| 54 |
+
for chunk in stream:
|
| 55 |
+
if chunk.choices[0].delta.content:
|
| 56 |
+
response_text += chunk.choices[0].delta.content
|
| 57 |
+
yield response_text
|
| 58 |
|
| 59 |
+
except Exception as e:
|
| 60 |
+
yield f"Error: {str(e)}"
|
| 61 |
|
| 62 |
|
| 63 |
chatbot = gr.ChatInterface(
|
|
|
|
| 77 |
)
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|
| 80 |
+
chatbot.queue(default_concurrency_limit=5).launch()
|