Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,46 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 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 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import groq
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
+
# Ensure the API key is set as an environment variable
|
| 6 |
+
api_key = os.getenv("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")
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
if not api_key:
|
| 9 |
+
raise RuntimeError("❌ Error: GROQ_API_KEY is not set. Please add it in Hugging Face 'Settings' → 'Secrets'.")
|
| 10 |
|
| 11 |
+
# Initialize the Groq client with the API key
|
| 12 |
+
client = groq.Client(api_key=api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Chatbot function using Groq API
|
| 15 |
+
def chat_with_groq(user_input, history):
|
| 16 |
+
messages = [{"role": "system", "content": "You are a helpful AI assistant."}]
|
| 17 |
+
for user_msg, bot_msg in history:
|
| 18 |
+
messages.append({"role": "user", "content": user_msg})
|
| 19 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 20 |
+
messages.append({"role": "user", "content": user_input})
|
| 21 |
|
| 22 |
+
response = client.chat.completions.create(
|
| 23 |
+
model="llama3-8b-8192", # Replace with the actual model you intend to use
|
| 24 |
+
messages=messages,
|
| 25 |
+
temperature=0.7
|
| 26 |
+
)
|
| 27 |
|
| 28 |
+
bot_reply = response.choices[0].message.content
|
| 29 |
+
history.append((user_input, bot_reply))
|
| 30 |
+
return history
|
| 31 |
|
| 32 |
+
# Create Gradio UI for the chatbot
|
| 33 |
+
with gr.Blocks() as demo:
|
| 34 |
+
gr.Markdown("# 🤖 Groq AI Chatbot")
|
| 35 |
+
chatbot = gr.Chatbot()
|
| 36 |
+
user_input = gr.Textbox(label="Type your message...")
|
| 37 |
+
state = gr.State([])
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
def respond(input_text, chat_history):
|
| 40 |
+
chat_history = chat_with_groq(input_text, chat_history)
|
| 41 |
+
return chat_history
|
| 42 |
|
| 43 |
+
user_input.submit(respond, [user_input, state], [chatbot])
|
| 44 |
|
| 45 |
+
# Launch Gradio app
|
| 46 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|