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