Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,36 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
for val in history:
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
demo = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from groq import Groq
|
| 3 |
|
| 4 |
+
# === API KEY (get from environment on HF Spaces) ===
|
| 5 |
+
import os
|
| 6 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
|
|
|
| 7 |
|
| 8 |
+
# === Initialize Groq client ===
|
| 9 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 10 |
|
| 11 |
+
# === Chatbot logic ===
|
| 12 |
+
def chat_with_groq(message, history):
|
| 13 |
+
messages = [{"role": "system", "content": "You are a helpful assistant."}]
|
| 14 |
+
for user_msg, bot_msg in history:
|
| 15 |
+
messages.append({"role": "user", "content": user_msg})
|
| 16 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
messages.append({"role": "user", "content": message})
|
| 18 |
+
|
| 19 |
+
try:
|
| 20 |
+
response = client.chat.completions.create(
|
| 21 |
+
model="llama3-8b-8192", # ✅ Supported Groq model
|
| 22 |
+
messages=messages,
|
| 23 |
+
temperature=0.7,
|
| 24 |
+
)
|
| 25 |
+
reply = response.choices[0].message.content
|
| 26 |
+
except Exception as e:
|
| 27 |
+
reply = f"Error: {e}"
|
| 28 |
+
|
| 29 |
+
return reply
|
| 30 |
+
|
| 31 |
+
# === Gradio interface ===
|
| 32 |
+
chatbot = gr.ChatInterface(fn=chat_with_groq, title="Groq Chatbot")
|
| 33 |
+
|
| 34 |
+
# === Launch app ===
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
if __name__ == "__main__":
|
| 36 |
+
chatbot.launch()
|