BilalCode commited on
Commit
411fbf0
·
verified ·
1 Parent(s): 0498494

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from groq import Groq
3
+ import os
4
+ import time
5
+
6
+ # =========================
7
+ # LOAD API KEY (HUGGING FACE SECRETS)
8
+ # =========================
9
+ api_key = os.getenv("GROQ_API_KEY")
10
+
11
+ if not api_key:
12
+ raise ValueError("GROQ_API_KEY not found in environment variables")
13
+
14
+ client = Groq(api_key=api_key.strip())
15
+
16
+ # =========================
17
+ # CHAT FUNCTION (STREAMING)
18
+ # =========================
19
+ def chat(message, history):
20
+ try:
21
+ messages = [
22
+ {
23
+ "role": "system",
24
+ "content": "You are a helpful AI assistant."
25
+ }
26
+ ]
27
+
28
+ # Add history
29
+ for user, bot in history:
30
+ messages.append({"role": "user", "content": user})
31
+ messages.append({"role": "assistant", "content": bot})
32
+
33
+ messages.append({"role": "user", "content": message})
34
+
35
+ # Streaming request
36
+ stream = client.chat.completions.create(
37
+ model="llama-3.3-70b-versatile",
38
+ messages=messages,
39
+ stream=True,
40
+ temperature=0.7
41
+ )
42
+
43
+ response_text = ""
44
+
45
+ # Stream output token by token
46
+ for chunk in stream:
47
+ if chunk.choices[0].delta.content:
48
+ token = chunk.choices[0].delta.content
49
+ response_text += token
50
+
51
+ # Slow typing effect
52
+ time.sleep(0.03)
53
+
54
+ yield response_text + "▌"
55
+
56
+ # Final clean output
57
+ yield response_text
58
+
59
+ except Exception as e:
60
+ yield f"Error: {str(e)}"
61
+
62
+ # =========================
63
+ # UI (PROFESSIONAL)
64
+ # =========================
65
+ demo = gr.ChatInterface(
66
+ fn=chat,
67
+ title="Streaming AI Chatbot using Groq",
68
+ description="AI chatbot with real-time response streaming",
69
+ )
70
+
71
+ # =========================
72
+ # LAUNCH APP
73
+ # =========================
74
+ demo.launch()