Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from groq import Groq
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# -----------------------------
|
| 6 |
+
# 1. Initialize Groq client
|
| 7 |
+
# -----------------------------
|
| 8 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 9 |
+
if not GROQ_API_KEY:
|
| 10 |
+
raise ValueError("Please set your GROQ_API_KEY in the Hugging Face Space secrets.")
|
| 11 |
+
|
| 12 |
+
groq_client = Groq(api_key=GROQ_API_KEY)
|
| 13 |
+
|
| 14 |
+
# -----------------------------
|
| 15 |
+
# 2. Define chatbot function
|
| 16 |
+
# -----------------------------
|
| 17 |
+
def chat_with_groq(message, history):
|
| 18 |
+
"""
|
| 19 |
+
Chat function for Gradio interface.
|
| 20 |
+
Uses Groq API (Llama3-70B) for conversational responses.
|
| 21 |
+
"""
|
| 22 |
+
if not message or message.strip() == "":
|
| 23 |
+
return history + [["", "Please enter a message to start the chat."]]
|
| 24 |
+
|
| 25 |
+
# Format conversation context
|
| 26 |
+
messages = []
|
| 27 |
+
for user_msg, bot_msg in history:
|
| 28 |
+
messages.append({"role": "user", "content": user_msg})
|
| 29 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 30 |
+
messages.append({"role": "user", "content": message})
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
response = groq_client.chat.completions.create(
|
| 34 |
+
model="llama3-70b-8192",
|
| 35 |
+
messages=messages,
|
| 36 |
+
temperature=0.7,
|
| 37 |
+
max_tokens=400,
|
| 38 |
+
)
|
| 39 |
+
bot_reply = response.choices[0].message["content"]
|
| 40 |
+
except Exception as e:
|
| 41 |
+
bot_reply = f"⚠️ Error: {str(e)}"
|
| 42 |
+
|
| 43 |
+
history.append((message, bot_reply))
|
| 44 |
+
return history
|
| 45 |
+
|
| 46 |
+
# -----------------------------
|
| 47 |
+
# 3. Build Gradio interface
|
| 48 |
+
# -----------------------------
|
| 49 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="gray")) as demo:
|
| 50 |
+
gr.Markdown("## 🤖 Chatbot Powered by Groq Llama3")
|
| 51 |
+
gr.Markdown("Ask me anything — I'm powered by the **Groq API** and **Gradio**.")
|
| 52 |
+
|
| 53 |
+
chatbot = gr.Chatbot(label="Chat with AI", height=500)
|
| 54 |
+
user_input = gr.Textbox(
|
| 55 |
+
placeholder="Type your message here...",
|
| 56 |
+
label="Your Message",
|
| 57 |
+
)
|
| 58 |
+
send_button = gr.Button("Send")
|
| 59 |
+
|
| 60 |
+
clear_button = gr.ClearButton([user_input, chatbot])
|
| 61 |
+
|
| 62 |
+
send_button.click(chat_with_groq, inputs=[user_input, chatbot], outputs=chatbot)
|
| 63 |
+
user_input.submit(chat_with_groq, inputs=[user_input, chatbot], outputs=chatbot)
|
| 64 |
+
|
| 65 |
+
gr.Markdown("🧠 Model: **llama3-70b-8192** via Groq API \nBuilt with ❤️ using Gradio.")
|
| 66 |
+
|
| 67 |
+
# -----------------------------
|
| 68 |
+
# 4. Launch the app
|
| 69 |
+
# -----------------------------
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
demo.launch()
|