adikwok commited on
Commit
a312592
Β·
verified Β·
1 Parent(s): 3a01f98

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -108
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import gradio as gr
2
  import requests
3
  import os
@@ -7,143 +8,154 @@ from datetime import datetime
7
 
8
  # Groq API Configuration
9
  API_URL = "https://api.groq.com/openai/v1/chat/completions"
10
- API_KEY = os.getenv("GROQ_API_KEY") or "your-api-key-here" # Fallback for testing
11
 
12
- def verify_api_key():
13
- if not API_KEY or API_KEY == "your-api-key-here":
14
- print("❌ No valid API key found")
15
- print("Please set GROQ_API_KEY environment variable")
16
- return False
17
- return True
18
 
19
  # In-memory chat history storage
20
  chat_history: List[Dict[str, str]] = []
21
 
22
- def groq_with_memory(message: str, topic: str = "general", summarize: bool = False) -> str:
23
- """Groq API call with chat history"""
24
- if not verify_api_key():
25
- return "❌ API key not configured - please set GROQ_API_KEY environment variable"
 
26
 
27
  if not message.strip():
28
- return "❌ Please enter a message"
29
 
30
  try:
31
- # Add to history
 
 
 
 
 
32
  chat_history.append({
33
  "role": "user",
34
- "content": message,
35
  "topic": topic,
36
  "timestamp": datetime.now().isoformat()
37
  })
38
 
39
- # Prepare messages (last 10)
40
- messages = [{"role": msg["role"], "content": msg["content"]}
41
- for msg in chat_history[-10:]]
 
 
42
 
43
  if summarize:
44
- messages.append({
45
- "role": "user",
46
- "content": f"Summarize our conversation about {topic}"
47
- })
48
 
49
- response = requests.post(
50
- API_URL,
51
- headers={
52
- "Authorization": f"Bearer {API_KEY}",
53
- "Content-Type": "application/json"
54
- },
55
- json={
56
- "model": "llama3-8b-8192",
57
- "messages": messages,
58
- "temperature": 0.7
59
- },
60
- timeout=30
61
- )
62
 
63
  if response.status_code == 200:
64
- reply = response.json()["choices"][0]["message"]["content"]
65
- chat_history.append({
66
- "role": "assistant",
67
- "content": reply,
68
- "topic": topic,
69
- "timestamp": datetime.now().isoformat()
70
- })
71
- return reply
72
- return f"❌ Error {response.status_code}: {response.text}"
73
-
 
 
 
74
  except Exception as e:
75
- return f"❌ Connection error: {str(e)}"
76
 
77
- def process_chat(message: str, chat_ui: list, topic: str) -> tuple:
78
- """Process chat message and update UI"""
79
- if not message.strip():
80
- return "", chat_ui
81
- reply = groq_with_memory(message, topic)
82
- return "", chat_ui + [(message, reply)]
83
 
84
- def view_history(topic: str = None) -> str:
85
- """View raw chat history"""
86
  if not chat_history:
87
- return "No history yet"
88
- filtered = [msg for msg in chat_history if not topic or msg["topic"] == topic]
89
- return "\n".join(f"{msg['timestamp']} - {msg['role']}: {msg['content']}" for msg in filtered)
 
 
 
 
90
 
91
- def clear_all_history():
92
- """Clear all chat history"""
93
- chat_history.clear()
94
- return "βœ… History cleared", []
 
 
 
 
 
 
 
 
 
 
95
 
96
  # Gradio Interface
97
- with gr.Blocks(title="Groq Chat", theme=gr.themes.Soft()) as demo:
98
- with gr.Tabs():
99
- # Chat Tab
100
- with gr.Tab("πŸ’¬ Chat"):
101
- chatbot = gr.Chatbot(height=400)
102
- with gr.Row():
103
- message = gr.Textbox(placeholder="Type your message...", scale=7)
104
- send = gr.Button("Send", variant="primary")
105
-
106
- with gr.Accordion("Settings", open=False):
107
- topic = gr.Textbox(label="Conversation Topic", value="general")
108
- gr.Markdown("**Note:** History is preserved until you clear it")
109
-
110
- clear_chat = gr.Button("Clear Chat UI")
111
- clear_history = gr.Button("⚠️ Clear ALL History", variant="stop")
112
- status = gr.Textbox(label="Status")
 
 
 
 
 
 
 
113
 
114
- # History Tab
115
- with gr.Tab("πŸ“œ History"):
116
- history_topic = gr.Textbox(label="Filter by topic")
117
- view_btn = gr.Button("View History")
118
- raw_history = gr.Textbox(label="Raw History", lines=15)
119
- view_btn.click(view_history, history_topic, raw_history)
 
 
 
 
 
120
 
121
- # API Info Tab
122
- with gr.Tab("ℹ️ API Info"):
123
- gr.Markdown(f"""
124
- **API Status:** {'βœ… Ready' if verify_api_key() else '❌ Not Configured'}
125
- **Endpoint:** `{API_URL}`
126
- **Models Available:**
127
- - `llama3-8b-8192`
128
- - `llama3-70b-8192`
129
- - `mixtral-8x7b-32768`
130
-
131
- **How to set up:**
132
- 1. Get API key from [Groq Cloud](https://console.groq.com/)
133
- 2. Set environment variable:
134
- ```bash
135
- export GROQ_API_KEY="your-api-key-here"
136
- ```
137
- """)
138
 
139
- # Event handlers
140
- message.submit(process_chat, [message, chatbot, topic], [message, chatbot])
141
- send.click(process_chat, [message, chatbot, topic], [message, chatbot])
142
- clear_chat.click(lambda: [], None, chatbot)
143
- clear_history.click(clear_all_history, outputs=[status, chatbot])
 
 
 
 
 
144
 
145
  if __name__ == "__main__":
146
- if verify_api_key():
147
- demo.launch(server_name="0.0.0.0", server_port=7860)
148
- else:
149
- print("Cannot start app - no valid API key found")
 
1
+
2
  import gradio as gr
3
  import requests
4
  import os
 
8
 
9
  # Groq API Configuration
10
  API_URL = "https://api.groq.com/openai/v1/chat/completions"
11
+ API_KEY = os.getenv("GROQ_API_KEY")
12
 
13
+ print(f"πŸ”‘ API Key Found: {'Yes' if API_KEY else 'No'}")
 
 
 
 
 
14
 
15
  # In-memory chat history storage
16
  chat_history: List[Dict[str, str]] = []
17
 
18
+ def groq_with_memory(message: str, topic: str = "general", summarize: bool = False, max_history: int = 10) -> str:
19
+ """Groq API call with chat history and optional summarization"""
20
+
21
+ if not API_KEY:
22
+ return "❌ No API Key found"
23
 
24
  if not message.strip():
25
+ return "❌ Empty message"
26
 
27
  try:
28
+ headers = {
29
+ "Authorization": f"Bearer {API_KEY}",
30
+ "Content-Type": "application/json"
31
+ }
32
+
33
+ # Add current message to history with timestamp and topic
34
  chat_history.append({
35
  "role": "user",
36
+ "content": message.strip(),
37
  "topic": topic,
38
  "timestamp": datetime.now().isoformat()
39
  })
40
 
41
+ # Limit history to max_history messages
42
+ recent_history = chat_history[-max_history:]
43
+
44
+ # Prepare messages for API call
45
+ messages = [{"role": msg["role"], "content": msg["content"]} for msg in recent_history]
46
 
47
  if summarize:
48
+ summary_prompt = f"Summarize this conversation about '{topic}':\n\n"
49
+ summary_prompt += "\n".join([f"{msg['role']}: {msg['content']}" for msg in recent_history])
50
+ messages.append({"role": "user", "content": summary_prompt})
 
51
 
52
+ payload = {
53
+ "model": "deepseek-r1-distill-llama-70b",
54
+ "messages": messages,
55
+ "max_tokens": 512,
56
+ "temperature": 0.7
57
+ }
58
+
59
+ response = requests.post(API_URL, headers=headers, json=payload, timeout=30)
 
 
 
 
 
60
 
61
  if response.status_code == 200:
62
+ result = response.json()
63
+ if "choices" in result and result["choices"]:
64
+ response_content = result["choices"][0]["message"]["content"]
65
+ chat_history.append({
66
+ "role": "assistant",
67
+ "content": response_content,
68
+ "topic": topic,
69
+ "timestamp": datetime.now().isoformat()
70
+ })
71
+ return response_content
72
+ return f"❌ No choices in response: {result}"
73
+ return f"❌ HTTP {response.status_code}: {response.text}"
74
+
75
  except Exception as e:
76
+ return f"❌ Error: {str(e)}"
77
 
78
+ def clear_history():
79
+ """Clear the chat history"""
80
+ chat_history.clear()
81
+ return "βœ… Chat history cleared"
 
 
82
 
83
+ def view_history(topic: str = None):
84
+ """View chat history with optional topic filter"""
85
  if not chat_history:
86
+ return "❌ No chat history available"
87
+
88
+ filtered = [msg for msg in chat_history if topic is None or msg["topic"] == topic]
89
+ return "\n".join(
90
+ f"[{msg['timestamp']}] {msg['role'].capitalize()}: {msg['content']}"
91
+ for msg in filtered
92
+ )
93
 
94
+ def manual_curl_example():
95
+ """Generate example curl command"""
96
+ if not API_KEY:
97
+ return "❌ No API key available"
98
+ return f"""curl -X POST "{API_URL}" \\
99
+ -H "Authorization: Bearer {API_KEY}" \\
100
+ -H "Content-Type: application/json" \\
101
+ -d '{{"model": "llama3-8b-8192", "messages": [{{"role": "user", "content": "Hello"}}]}}'
102
+ """
103
+
104
+ def process_message(message: str, topic: str, summarize: bool):
105
+ """Process message and clear input"""
106
+ response = groq_with_memory(message, topic, summarize)
107
+ return response, ""
108
 
109
  # Gradio Interface
110
+ with gr.Blocks(title="Groq Chat Debugger") as demo:
111
+ gr.Markdown("## πŸ’¬ Groq API Chat Interface")
112
+
113
+ with gr.Tab("Chat"):
114
+ with gr.Row():
115
+ with gr.Column():
116
+ msg_input = gr.Textbox(
117
+ label="Your Message",
118
+ value="Hello, how can you help me today?"
119
+ )
120
+ topic_input = gr.Textbox(
121
+ label="Topic",
122
+ value="general"
123
+ )
124
+ summarize_cb = gr.Checkbox(label="Summarize conversation")
125
+ send_btn = gr.Button("Send", variant="primary")
126
+
127
+ with gr.Column():
128
+ response_output = gr.Textbox(
129
+ label="Response",
130
+ lines=10,
131
+ interactive=False
132
+ )
133
 
134
+ send_btn.click(
135
+ process_message,
136
+ inputs=[msg_input, topic_input, summarize_cb],
137
+ outputs=[response_output, msg_input]
138
+ )
139
+
140
+ with gr.Tab("History"):
141
+ history_topic = gr.Textbox(label="Filter by Topic")
142
+ view_btn = gr.Button("View History")
143
+ clear_btn = gr.Button("Clear History", variant="stop")
144
+ history_output = gr.Textbox(label="Chat History", lines=10)
145
 
146
+ view_btn.click(view_history, inputs=[history_topic], outputs=[history_output])
147
+ clear_btn.click(clear_history, outputs=[history_output])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
 
149
+ with gr.Tab("API Tools"):
150
+ gr.Markdown(f"""
151
+ **API Status:** {'βœ… Ready' if API_KEY else '❌ No API Key'}
152
+ **Endpoint:** `{API_URL}`
153
+ **Models:** `llama3-8b-8192`, `llama3-70b-8192`, `mixtral-8x7b-32768`
154
+ """)
155
+
156
+ curl_btn = gr.Button("Generate cURL Example")
157
+ curl_output = gr.Code(label="cURL Command", language="shell")
158
+ curl_btn.click(manual_curl_example, outputs=[curl_output])
159
 
160
  if __name__ == "__main__":
161
+ demo.launch(server_name="0.0.0.0", server_port=7860)