Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from groq import Groq
|
| 4 |
+
|
| 5 |
+
# ==============================
|
| 6 |
+
# Add your Groq API Key here
|
| 7 |
+
# (or set in HuggingFace Secrets)
|
| 8 |
+
# ==============================
|
| 9 |
+
GROQ_API_KEY = os.getenv("Access")
|
| 10 |
+
|
| 11 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 12 |
+
|
| 13 |
+
# ------------------------------
|
| 14 |
+
# Chat Function
|
| 15 |
+
# ------------------------------
|
| 16 |
+
def chatbot(message, history):
|
| 17 |
+
|
| 18 |
+
messages = []
|
| 19 |
+
|
| 20 |
+
# previous conversation
|
| 21 |
+
for user, bot in history:
|
| 22 |
+
messages.append({"role": "user", "content": user})
|
| 23 |
+
messages.append({"role": "assistant", "content": bot})
|
| 24 |
+
|
| 25 |
+
# new message
|
| 26 |
+
messages.append({"role": "user", "content": message})
|
| 27 |
+
|
| 28 |
+
# Groq API call
|
| 29 |
+
completion = client.chat.completions.create(
|
| 30 |
+
model="llama-3.1-8b-instant",
|
| 31 |
+
messages=messages,
|
| 32 |
+
temperature=0.7,
|
| 33 |
+
max_tokens=1024,
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
reply = completion.choices[0].message.content
|
| 37 |
+
|
| 38 |
+
return reply
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# ------------------------------
|
| 42 |
+
# Gradio Interface
|
| 43 |
+
# ------------------------------
|
| 44 |
+
demo = gr.ChatInterface(
|
| 45 |
+
fn=chatbot,
|
| 46 |
+
title="🤖 Groq AI Chatbot",
|
| 47 |
+
description="Chatbot powered by Groq API + Gradio",
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
demo.launch()
|