feat: Change AI model API dependency to GROQ
Browse files- .gitignore +11 -0
- .python-version +1 -0
- app.py +31 -31
- pyproject.toml +11 -0
- uv.lock +0 -0
.gitignore
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Python-generated files
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[oc]
|
| 4 |
+
build/
|
| 5 |
+
dist/
|
| 6 |
+
wheels/
|
| 7 |
+
*.egg-info
|
| 8 |
+
|
| 9 |
+
# Virtual environments
|
| 10 |
+
.venv
|
| 11 |
+
.env
|
.python-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
3.10
|
app.py
CHANGED
|
@@ -1,51 +1,57 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
| 3 |
|
|
|
|
| 4 |
|
| 5 |
def respond(
|
| 6 |
message,
|
| 7 |
-
history
|
| 8 |
system_message,
|
| 9 |
max_tokens,
|
| 10 |
temperature,
|
| 11 |
top_p,
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
):
|
| 14 |
-
""
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
|
|
|
|
|
|
| 23 |
messages.append({"role": "user", "content": message})
|
| 24 |
|
| 25 |
-
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 29 |
max_tokens=max_tokens,
|
| 30 |
-
stream=True,
|
| 31 |
temperature=temperature,
|
| 32 |
top_p=top_p,
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
|
| 42 |
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
chatbot = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
-
type="messages",
|
| 49 |
additional_inputs=[
|
| 50 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 51 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
|
@@ -60,11 +66,5 @@ chatbot = gr.ChatInterface(
|
|
| 60 |
],
|
| 61 |
)
|
| 62 |
|
| 63 |
-
with gr.Blocks() as demo:
|
| 64 |
-
with gr.Sidebar():
|
| 65 |
-
gr.LoginButton()
|
| 66 |
-
chatbot.render()
|
| 67 |
-
|
| 68 |
-
|
| 69 |
if __name__ == "__main__":
|
| 70 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
+
load_dotenv()
|
| 7 |
|
| 8 |
def respond(
|
| 9 |
message,
|
| 10 |
+
history,
|
| 11 |
system_message,
|
| 12 |
max_tokens,
|
| 13 |
temperature,
|
| 14 |
top_p,
|
|
|
|
| 15 |
):
|
| 16 |
+
api_key = os.getenv("GROQ_API_KEY")
|
| 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 |
+
)
|
| 24 |
|
| 25 |
+
def sanitize_messages(history):
|
| 26 |
+
return [
|
| 27 |
+
{"role": m["role"], "content": m["content"]}
|
| 28 |
+
for m in history
|
| 29 |
+
]
|
| 30 |
|
| 31 |
+
messages = [{"role": "system", "content": system_message}]
|
| 32 |
+
messages.extend(sanitize_messages(history))
|
| 33 |
messages.append({"role": "user", "content": message})
|
| 34 |
|
| 35 |
+
response_text = ""
|
| 36 |
|
| 37 |
+
stream = client.chat.completions.create(
|
| 38 |
+
model="qwen/qwen3-32b",
|
| 39 |
+
messages=messages,
|
| 40 |
max_tokens=max_tokens,
|
|
|
|
| 41 |
temperature=temperature,
|
| 42 |
top_p=top_p,
|
| 43 |
+
stream=True,
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
for chunk in stream:
|
| 47 |
+
if chunk.choices[0].delta.content:
|
| 48 |
+
response_text += chunk.choices[0].delta.content
|
| 49 |
+
yield response_text
|
| 50 |
|
|
|
|
|
|
|
| 51 |
|
| 52 |
|
|
|
|
|
|
|
|
|
|
| 53 |
chatbot = gr.ChatInterface(
|
| 54 |
respond,
|
|
|
|
| 55 |
additional_inputs=[
|
| 56 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 57 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
|
|
|
| 66 |
],
|
| 67 |
)
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
if __name__ == "__main__":
|
| 70 |
+
chatbot.launch()
|
pyproject.toml
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "chatbot"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = "Add your description here"
|
| 5 |
+
readme = "README.md"
|
| 6 |
+
requires-python = ">=3.10"
|
| 7 |
+
dependencies = [
|
| 8 |
+
"dotenv>=0.9.9",
|
| 9 |
+
"gradio[oauth]>=6.4.0",
|
| 10 |
+
"openai>=2.15.0",
|
| 11 |
+
]
|
uv.lock
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|