Delete app.py
Browse files
app.py
DELETED
|
@@ -1,68 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import gradio as gr
|
| 3 |
-
from groq import Groq
|
| 4 |
-
|
| 5 |
-
# -----------------------------
|
| 6 |
-
# Environment Variable Check
|
| 7 |
-
# -----------------------------
|
| 8 |
-
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 9 |
-
if not GROQ_API_KEY:
|
| 10 |
-
raise RuntimeError(
|
| 11 |
-
"GROQ_API_KEY environment variable not set. "
|
| 12 |
-
"Please set it before running the app."
|
| 13 |
-
)
|
| 14 |
-
|
| 15 |
-
client = Groq(api_key=GROQ_API_KEY)
|
| 16 |
-
MODEL_NAME = "llama-3.3-70b-versatile"
|
| 17 |
-
|
| 18 |
-
# -----------------------------
|
| 19 |
-
# Chat Function
|
| 20 |
-
# -----------------------------
|
| 21 |
-
def chat_fn(message, history):
|
| 22 |
-
# Ensure history is a list
|
| 23 |
-
if history is None:
|
| 24 |
-
history = []
|
| 25 |
-
|
| 26 |
-
# Convert history to Groq messages
|
| 27 |
-
messages = []
|
| 28 |
-
for user_msg, bot_msg in history:
|
| 29 |
-
messages.append({"role": "user", "content": str(user_msg)})
|
| 30 |
-
messages.append({"role": "assistant", "content": str(bot_msg)})
|
| 31 |
-
|
| 32 |
-
# Add current user message
|
| 33 |
-
messages.append({"role": "user", "content": str(message)})
|
| 34 |
-
|
| 35 |
-
try:
|
| 36 |
-
response = client.chat.completions.create(
|
| 37 |
-
model=MODEL_NAME,
|
| 38 |
-
messages=messages
|
| 39 |
-
)
|
| 40 |
-
# Ensure reply is a string
|
| 41 |
-
reply = str(response.choices[0].message.content or "")
|
| 42 |
-
|
| 43 |
-
# Update history
|
| 44 |
-
history = history + [(str(message), reply)]
|
| 45 |
-
|
| 46 |
-
return reply, history
|
| 47 |
-
|
| 48 |
-
except Exception as e:
|
| 49 |
-
err_msg = f"⚠️ Error: {str(e)}"
|
| 50 |
-
history = history + [(str(message), err_msg)]
|
| 51 |
-
return err_msg, history
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
# -----------------------------
|
| 55 |
-
# Gradio UI
|
| 56 |
-
# -----------------------------
|
| 57 |
-
chat_ui = gr.ChatInterface(
|
| 58 |
-
fn=chat_fn,
|
| 59 |
-
title="Groq-Powered Chatbot",
|
| 60 |
-
description="A simple chatbot built with Gradio and Groq LLMs.",
|
| 61 |
-
examples=[
|
| 62 |
-
"What is machine learning?",
|
| 63 |
-
"Explain recursion in simple terms",
|
| 64 |
-
"How does a neural network work?"
|
| 65 |
-
],
|
| 66 |
-
)
|
| 67 |
-
chat_ui.launch()
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|