ryanpereira commited on
Commit
bc4a33a
·
verified ·
1 Parent(s): d0f9daf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -33
app.py CHANGED
@@ -1,54 +1,56 @@
1
- # GOAL: Building an ai powered chatbot
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 GROQ_API_KEY from .env
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" # or "mixtral-8x7b-32768", etc.
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
- {"role": "system", "content": "You are a helpful LLM teacher."},
24
- {"role": "user", "content": message}
25
- ]
26
-
27
- response = requests.post(
28
- GROQ_API_URL,
29
- headers=headers,
30
- json={
31
- "model": MODEL,
32
- "messages": messages
33
- }
34
- )
35
-
36
- if response.status_code == 200:
37
- completion = response.json()
38
- return completion["choices"][0]["message"]["content"]
39
- else:
40
- return f"Error: {response.status_code}\n{response.text}"
41
-
42
- # frontend: Gradio
 
 
 
 
 
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
- demo.launch()
 
 
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)