adikwok commited on
Commit
7103644
Β·
verified Β·
1 Parent(s): 862323b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -123
app.py CHANGED
@@ -1,195 +1,189 @@
1
  import gradio as gr
2
  import requests
3
  import os
4
- from typing import List, Tuple, Any
5
 
6
- # Configuration - Set API key in Hugging Face Spaces Secrets
7
- API_URL = "https://api.deepseek.com/v1/chat/completions"
8
- API_KEY = os.getenv("DEEPSEEK_API_KEY")
9
-
10
- # Debug API key loading
11
- print(f"πŸ”‘ API Key Status: {'βœ… Loaded' if API_KEY else '❌ Missing'}")
12
- if API_KEY:
13
- print(f"πŸ”‘ API Key Preview: {API_KEY[:10]}...")
14
-
15
- # Validate API key at startup
16
- if not API_KEY:
17
- print("❌ ERROR: DEEPSEEK_API_KEY environment variable not found!")
18
- print("πŸ’‘ Please set your API key:")
19
- print(" - Local: export DEEPSEEK_API_KEY='your-key'")
20
- print(" - HF Spaces: Add to Repository Secrets")
21
-
22
- headers = {
23
- "Authorization": f"Bearer {API_KEY}",
24
- "Content-Type": "application/json"
 
 
 
 
 
 
 
25
  }
26
 
27
- def test_api_connection():
28
- """Test if API is accessible"""
29
- if not API_KEY:
30
- return False, "API key not found"
31
 
32
- try:
33
- response = requests.post(
34
- API_URL,
35
- headers=headers,
36
- json={
37
- "model": "deepseek-chat",
38
- "messages": [{"role": "user", "content": "test"}],
39
- "max_tokens": 10
40
- },
41
- timeout=10
42
- )
43
- return response.status_code == 200, f"Status: {response.status_code}"
44
- except Exception as e:
45
- return False, str(e)
46
-
47
- def query_deepseek(message: str, history: List[Tuple[str, str]]) -> str:
48
- """Query DeepSeek API with robust error handling"""
49
 
50
- # Check API key first
51
- if not API_KEY:
52
- return "❌ **API Key Missing**\n\nPlease set DEEPSEEK_API_KEY environment variable:\n- Local: `export DEEPSEEK_API_KEY='your-key'`\n- HF Spaces: Add to Repository Secrets"
53
 
54
  try:
55
- # Build conversation history
56
  messages = [{"role": "system", "content": "You are a helpful assistant."}]
57
-
58
- # Add chat history
59
  for user_msg, bot_msg in history:
60
- if user_msg and bot_msg: # Skip empty messages
61
  messages.extend([
62
  {"role": "user", "content": user_msg},
63
  {"role": "assistant", "content": bot_msg}
64
  ])
65
-
66
- # Add current message
67
  messages.append({"role": "user", "content": message})
68
 
69
- # API request
 
 
 
 
 
 
 
 
 
70
  response = requests.post(
71
- API_URL,
72
  headers=headers,
73
  json={
74
- "model": "deepseek-chat",
75
  "messages": messages,
76
  "temperature": 0.7,
77
- "max_tokens": 1024,
78
- "stream": False
79
  },
80
  timeout=30
81
  )
82
 
83
- # Detailed error handling
84
  if response.status_code == 401:
85
- return "❌ **Authentication Error**\nInvalid API key. Please check your DEEPSEEK_API_KEY."
 
 
86
  elif response.status_code == 403:
87
- return "❌ **Access Denied**\nAPI key doesn't have permission or quota exceeded."
88
- elif response.status_code == 404:
89
- return "❌ **API Not Found**\nCheck if the API URL is correct: https://api.deepseek.com/v1/chat/completions"
90
  elif response.status_code == 429:
91
- return "❌ **Rate Limited**\nToo many requests. Please wait and try again."
92
  elif response.status_code >= 500:
93
- return f"❌ **Server Error**\nDeepSeek server error (HTTP {response.status_code}). Try again later."
94
 
95
  response.raise_for_status()
96
-
97
- # Parse response
98
  result = response.json()
 
99
  if "choices" in result and len(result["choices"]) > 0:
100
  return result["choices"][0]["message"]["content"]
101
  else:
102
- return f"❌ **Unexpected Response**\n```json\n{result}\n```"
103
 
104
  except requests.exceptions.Timeout:
105
- return "❌ **Timeout Error**\nRequest took too long. Please try again."
106
- except requests.exceptions.ConnectionError:
107
- return "❌ **Connection Error**\nCannot connect to DeepSeek API. Check your internet connection."
108
- except requests.exceptions.RequestException as e:
109
- return f"❌ **Request Error**\n{str(e)}"
110
  except Exception as e:
111
- return f"❌ **Unexpected Error**\n{str(e)}"
 
 
 
 
 
 
 
 
 
112
 
113
  def create_interface():
114
- # Test API connection at startup
115
- api_ok, api_msg = test_api_connection()
116
- status_msg = f"🟒 API Connected: {api_msg}" if api_ok else f"πŸ”΄ API Issue: {api_msg}"
117
 
118
- with gr.Blocks(
119
- title="DeepSeek Chat",
120
- theme=gr.themes.Soft(),
121
- analytics_enabled=False
122
- ) as demo:
123
 
124
- gr.Markdown("# πŸ€– DeepSeek API Chat")
125
- gr.Markdown(f"**Status:** {status_msg}")
 
 
 
 
 
126
 
127
- chatbot = gr.Chatbot(
128
- label="Conversation",
129
- bubble_full_width=False,
130
- height=500
131
- )
132
 
133
  with gr.Row():
134
  msg = gr.Textbox(
135
- label="Your Message",
136
- placeholder="Type your message here...",
137
- scale=4,
138
- lines=2
139
  )
140
- with gr.Column(scale=1):
141
- submit = gr.Button("Send πŸ“€", variant="primary")
142
- clear = gr.Button("Clear πŸ—‘οΈ")
143
-
144
- # Chat handler
145
- def respond(message: str, chat_history: List[List[str]]) -> Tuple[str, List[List[str]]]:
146
  if not message.strip():
147
  return "", chat_history
148
 
149
- # Convert to tuple format for API
150
  history_tuples = [(h[0], h[1]) for h in chat_history if len(h) >= 2]
151
- bot_message = query_deepseek(message.strip(), history_tuples)
152
  chat_history.append([message, bot_message])
153
  return "", chat_history
154
-
155
  # Event handlers
156
- msg.submit(respond, [msg, chatbot], [msg, chatbot])
157
- submit.click(respond, [msg, chatbot], [msg, chatbot])
158
- clear.click(lambda: [], None, chatbot, queue=False)
159
 
160
  # Examples
161
  gr.Examples(
162
  examples=[
163
- ["Hello! Can you introduce yourself?"],
164
- ["Explain quantum computing in simple terms"],
165
- ["Write a Python function to calculate Fibonacci numbers"],
166
- ["What are the key features of DeepSeek?"],
167
- ["Help me debug this error: 'list index out of range'"]
168
  ],
169
  inputs=msg
170
  )
171
 
172
- # Footer
173
  gr.Markdown("""
174
  ---
175
- **Troubleshooting:**
176
- - ❌ API Key Missing β†’ Set `DEEPSEEK_API_KEY` environment variable
177
- - ❌ 401 Error β†’ Invalid API key
178
- - ❌ 403 Error β†’ No permission or quota exceeded
179
- - ❌ 404 Error β†’ Wrong API URL
 
 
 
 
 
 
 
 
 
 
 
 
180
  """)
181
 
182
  return demo
183
 
184
- # Launch configuration
185
  if __name__ == "__main__":
186
- print("πŸš€ Starting DeepSeek Gradio App...")
187
-
188
  app = create_interface()
189
- app.launch(
190
- server_name="0.0.0.0",
191
- server_port=7860,
192
- show_error=True,
193
- share=False, # Set to True if you want public link
194
- quiet=False
195
- )
 
1
  import gradio as gr
2
  import requests
3
  import os
4
+ from typing import List, Tuple
5
 
6
+ # Multiple API options
7
+ APIS = {
8
+ "deepseek": {
9
+ "url": "https://api.deepseek.com/v1/chat/completions",
10
+ "key": os.getenv("DEEPSEEK_API_KEY"),
11
+ "model": "deepseek-chat",
12
+ "free": False
13
+ },
14
+ "groq": {
15
+ "url": "https://api.groq.com/openai/v1/chat/completions",
16
+ "key": os.getenv("GROQ_API_KEY"),
17
+ "model": "llama3-8b-8192", # Free tier available
18
+ "free": True
19
+ },
20
+ "together": {
21
+ "url": "https://api.together.xyz/v1/chat/completions",
22
+ "key": os.getenv("TOGETHER_API_KEY"),
23
+ "model": "meta-llama/Llama-2-7b-chat-hf",
24
+ "free": True # $5 free credit
25
+ },
26
+ "openrouter": {
27
+ "url": "https://openrouter.ai/api/v1/chat/completions",
28
+ "key": os.getenv("OPENROUTER_API_KEY"),
29
+ "model": "google/gemma-7b-it:free", # Free model
30
+ "free": True
31
+ }
32
  }
33
 
34
+ def query_api(message: str, history: List[Tuple[str, str]], api_choice: str) -> str:
35
+ """Query selected API with error handling"""
 
 
36
 
37
+ if api_choice not in APIS:
38
+ return f"❌ Unknown API: {api_choice}"
39
+
40
+ config = APIS[api_choice]
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
+ if not config["key"]:
43
+ return f"❌ **API Key Missing for {api_choice.upper()}**\n\nSet environment variable: `{api_choice.upper()}_API_KEY`"
 
44
 
45
  try:
46
+ # Build messages
47
  messages = [{"role": "system", "content": "You are a helpful assistant."}]
 
 
48
  for user_msg, bot_msg in history:
49
+ if user_msg and bot_msg:
50
  messages.extend([
51
  {"role": "user", "content": user_msg},
52
  {"role": "assistant", "content": bot_msg}
53
  ])
 
 
54
  messages.append({"role": "user", "content": message})
55
 
56
+ headers = {
57
+ "Authorization": f"Bearer {config['key']}",
58
+ "Content-Type": "application/json"
59
+ }
60
+
61
+ # Add special headers for some APIs
62
+ if api_choice == "openrouter":
63
+ headers["HTTP-Referer"] = "https://your-app.com"
64
+ headers["X-Title"] = "Gradio Chat"
65
+
66
  response = requests.post(
67
+ config["url"],
68
  headers=headers,
69
  json={
70
+ "model": config["model"],
71
  "messages": messages,
72
  "temperature": 0.7,
73
+ "max_tokens": 1024
 
74
  },
75
  timeout=30
76
  )
77
 
78
+ # Handle different error codes
79
  if response.status_code == 401:
80
+ return f"❌ **Authentication Error ({api_choice})**\nInvalid API key"
81
+ elif response.status_code == 402:
82
+ return f"πŸ’³ **Payment Required ({api_choice})**\nAccount needs billing setup or credits"
83
  elif response.status_code == 403:
84
+ return f"❌ **Access Denied ({api_choice})**\nPermission denied or quota exceeded"
 
 
85
  elif response.status_code == 429:
86
+ return f"❌ **Rate Limited ({api_choice})**\nToo many requests, please wait"
87
  elif response.status_code >= 500:
88
+ return f"❌ **Server Error ({api_choice})**\nAPI server error, try again later"
89
 
90
  response.raise_for_status()
 
 
91
  result = response.json()
92
+
93
  if "choices" in result and len(result["choices"]) > 0:
94
  return result["choices"][0]["message"]["content"]
95
  else:
96
+ return f"❌ **No Response from {api_choice}**\n```json\n{result}\n```"
97
 
98
  except requests.exceptions.Timeout:
99
+ return f"❌ **Timeout ({api_choice})**\nRequest took too long"
 
 
 
 
100
  except Exception as e:
101
+ return f"❌ **Error ({api_choice})**\n{str(e)}"
102
+
103
+ def get_available_apis():
104
+ """Get list of APIs with their status"""
105
+ available = []
106
+ for name, config in APIS.items():
107
+ status = "🟒" if config["key"] else "πŸ”΄"
108
+ free_tag = " (FREE)" if config["free"] else " (PAID)"
109
+ available.append(f"{status} {name.upper()}{free_tag}")
110
+ return available
111
 
112
  def create_interface():
113
+ available_apis = get_available_apis()
 
 
114
 
115
+ with gr.Blocks(title="Multi-API Chat", theme=gr.themes.Soft()) as demo:
116
+ gr.Markdown("# πŸ€– Multi-API Chat Interface")
117
+ gr.Markdown("**Available APIs:**\n" + "\n".join([f"- {api}" for api in available_apis]))
 
 
118
 
119
+ with gr.Row():
120
+ api_choice = gr.Dropdown(
121
+ choices=list(APIS.keys()),
122
+ value="groq", # Default to free option
123
+ label="Select API",
124
+ info="Choose your AI API provider"
125
+ )
126
 
127
+ chatbot = gr.Chatbot(label="Conversation", height=500)
 
 
 
 
128
 
129
  with gr.Row():
130
  msg = gr.Textbox(
131
+ label="Message",
132
+ placeholder="Type your message...",
133
+ scale=4
 
134
  )
135
+ send_btn = gr.Button("Send πŸ“€", variant="primary")
136
+
137
+ clear_btn = gr.Button("Clear Chat πŸ—‘οΈ")
138
+
139
+ def respond(message: str, chat_history: List[List[str]], api: str):
 
140
  if not message.strip():
141
  return "", chat_history
142
 
 
143
  history_tuples = [(h[0], h[1]) for h in chat_history if len(h) >= 2]
144
+ bot_message = query_api(message.strip(), history_tuples, api)
145
  chat_history.append([message, bot_message])
146
  return "", chat_history
147
+
148
  # Event handlers
149
+ msg.submit(respond, [msg, chatbot, api_choice], [msg, chatbot])
150
+ send_btn.click(respond, [msg, chatbot, api_choice], [msg, chatbot])
151
+ clear_btn.click(lambda: [], None, chatbot)
152
 
153
  # Examples
154
  gr.Examples(
155
  examples=[
156
+ ["Hello! How are you?"],
157
+ ["Explain machine learning simply"],
158
+ ["Write a Python function to reverse a string"],
159
+ ["What's the difference between AI and ML?"]
 
160
  ],
161
  inputs=msg
162
  )
163
 
 
164
  gr.Markdown("""
165
  ---
166
+ ## πŸ”§ Setup Instructions:
167
+
168
+ **Free Options (Recommended for testing):**
169
+ 1. **Groq** (Free): Get API key at [console.groq.com](https://console.groq.com)
170
+ 2. **Together** (Free $5): Get key at [api.together.xyz](https://api.together.xyz)
171
+ 3. **OpenRouter** (Free models): Get key at [openrouter.ai](https://openrouter.ai)
172
+
173
+ **Paid Options:**
174
+ 4. **DeepSeek** (Paid): Needs billing setup at [platform.deepseek.com](https://platform.deepseek.com)
175
+
176
+ **Set Environment Variables:**
177
+ ```bash
178
+ export GROQ_API_KEY="your-key"
179
+ export TOGETHER_API_KEY="your-key"
180
+ export OPENROUTER_API_KEY="your-key"
181
+ export DEEPSEEK_API_KEY="your-key"
182
+ ```
183
  """)
184
 
185
  return demo
186
 
 
187
  if __name__ == "__main__":
 
 
188
  app = create_interface()
189
+ app.launch(server_name="0.0.0.0", server_port=7860)