Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,56 @@
|
|
| 1 |
-
#
|
| 2 |
-
# GOAL: Building an AI-powered chatbot using GROQ API
|
| 3 |
import gradio as gr
|
| 4 |
import requests
|
| 5 |
import os
|
| 6 |
-
from groq import Groq
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
|
| 9 |
-
load_dotenv() # Load
|
| 10 |
|
| 11 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
|
|
|
|
|
|
|
|
|
| 12 |
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 13 |
-
MODEL = "llama3-8b-8192"
|
| 14 |
|
| 15 |
headers = {
|
| 16 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 17 |
"Content-Type": "application/json"
|
| 18 |
}
|
| 19 |
|
| 20 |
-
# backend: Python
|
| 21 |
def echo(message, history):
|
| 22 |
-
messages = [
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
demo = gr.ChatInterface(
|
| 44 |
fn=echo,
|
| 45 |
type="messages",
|
| 46 |
-
examples=[
|
| 47 |
-
"I want to learn about LLMs",
|
| 48 |
-
"What is NLP",
|
| 49 |
-
"What is RAG"
|
| 50 |
-
],
|
| 51 |
title="LLM Mentor"
|
| 52 |
)
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
| 1 |
+
# app.py
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import os
|
|
|
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
+
load_dotenv() # Load .env if running locally
|
| 8 |
|
| 9 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 10 |
+
if not GROQ_API_KEY:
|
| 11 |
+
raise ValueError("Missing GROQ_API_KEY. Set it in Hugging Face Secrets or a .env file.")
|
| 12 |
+
|
| 13 |
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 14 |
+
MODEL = "llama3-8b-8192"
|
| 15 |
|
| 16 |
headers = {
|
| 17 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 18 |
"Content-Type": "application/json"
|
| 19 |
}
|
| 20 |
|
|
|
|
| 21 |
def echo(message, history):
|
| 22 |
+
messages = [{"role": "system", "content": "You are a helpful LLM teacher."}]
|
| 23 |
+
|
| 24 |
+
# Add chat history for better context
|
| 25 |
+
for user, bot in history:
|
| 26 |
+
messages.append({"role": "user", "content": user})
|
| 27 |
+
messages.append({"role": "assistant", "content": bot})
|
| 28 |
+
|
| 29 |
+
messages.append({"role": "user", "content": message})
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
response = requests.post(
|
| 33 |
+
GROQ_API_URL,
|
| 34 |
+
headers=headers,
|
| 35 |
+
json={
|
| 36 |
+
"model": MODEL,
|
| 37 |
+
"messages": messages
|
| 38 |
+
}
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
if response.status_code == 200:
|
| 42 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 43 |
+
else:
|
| 44 |
+
return f"Error: {response.status_code}\n{response.text}"
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return f"Exception occurred: {str(e)}"
|
| 47 |
+
|
| 48 |
demo = gr.ChatInterface(
|
| 49 |
fn=echo,
|
| 50 |
type="messages",
|
| 51 |
+
examples=["I want to learn about LLMs", "What is NLP?", "What is RAG?"],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
title="LLM Mentor"
|
| 53 |
)
|
| 54 |
|
| 55 |
+
# Disable SSR to avoid Hugging Face crash
|
| 56 |
+
demo.launch(ssr_mode=False)
|