File size: 1,395 Bytes
c92a360 d529580 c92a360 d529580 c92a360 d529580 c92a360 d529580 c92a360 d529580 c92a360 d529580 c92a360 d529580 c92a360 | 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | import os
import gradio as gr
from groq import Groq
# ==============================
# SET YOUR GROQ API KEY
# ==============================
# In Hugging Face:
# Go to Settings → Secrets → Add new secret
# Name: GROQ_API_KEY
# Value: your_api_key_here
client = Groq(
api_key=os.environ.get("Keypass")
)
# ==============================
# CHAT FUNCTION
# ==============================
def chatbot_response(message, history):
try:
messages = []
# Add chat history
for human, assistant in history:
messages.append({"role": "user", "content": human})
messages.append({"role": "assistant", "content": assistant})
# Add current message
messages.append({"role": "user", "content": message})
completion = client.chat.completions.create(
model="llama3-70b-8192",
messages=messages,
temperature=0.7,
max_tokens=1024,
)
response = completion.choices[0].message.content
return response
except Exception as e:
return f"Error: {str(e)}"
# ==============================
# GRADIO INTERFACE
# ==============================
demo = gr.ChatInterface(
fn=chatbot_response,
title="🚀 Groq AI Chatbot",
description="Chatbot powered by Groq API + LLaMA3",
theme="soft"
)
if __name__ == "__main__":
demo.launch() |