Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from groq import Groq
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# 🔐 Set your Groq API key here OR use Hugging Face Secrets
|
| 6 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 7 |
+
|
| 8 |
+
# Initialize client
|
| 9 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 10 |
+
|
| 11 |
+
# Chat function
|
| 12 |
+
def chatbot(message, history):
|
| 13 |
+
messages = []
|
| 14 |
+
|
| 15 |
+
# Add previous conversation
|
| 16 |
+
for user, bot in history:
|
| 17 |
+
messages.append({"role": "user", "content": user})
|
| 18 |
+
messages.append({"role": "assistant", "content": bot})
|
| 19 |
+
|
| 20 |
+
# Add current message
|
| 21 |
+
messages.append({"role": "user", "content": message})
|
| 22 |
+
|
| 23 |
+
# Call Groq API
|
| 24 |
+
response = client.chat.completions.create(
|
| 25 |
+
model="llama3-70b-8192", # fast + powerful
|
| 26 |
+
messages=messages,
|
| 27 |
+
temperature=0.7,
|
| 28 |
+
max_tokens=512,
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
reply = response.choices[0].message.content
|
| 32 |
+
return reply
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
# Gradio UI
|
| 36 |
+
demo = gr.ChatInterface(
|
| 37 |
+
fn=chatbot,
|
| 38 |
+
title="💬 AI Chatbot (Groq + Hugging Face)",
|
| 39 |
+
description="Ask anything! Powered by Groq API ⚡",
|
| 40 |
+
theme="soft"
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# Run app
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
demo.launch()
|