Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,195 +1,189 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
-
from typing import List, Tuple
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
}
|
| 26 |
|
| 27 |
-
def
|
| 28 |
-
"""
|
| 29 |
-
if not API_KEY:
|
| 30 |
-
return False, "API key not found"
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 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 |
-
|
| 51 |
-
|
| 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
|
| 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:
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
response = requests.post(
|
| 71 |
-
|
| 72 |
headers=headers,
|
| 73 |
json={
|
| 74 |
-
"model": "
|
| 75 |
"messages": messages,
|
| 76 |
"temperature": 0.7,
|
| 77 |
-
"max_tokens": 1024
|
| 78 |
-
"stream": False
|
| 79 |
},
|
| 80 |
timeout=30
|
| 81 |
)
|
| 82 |
|
| 83 |
-
#
|
| 84 |
if response.status_code == 401:
|
| 85 |
-
return "β **Authentication Error**\nInvalid API key
|
|
|
|
|
|
|
| 86 |
elif response.status_code == 403:
|
| 87 |
-
return "β **Access Denied**\
|
| 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
|
| 92 |
elif response.status_code >= 500:
|
| 93 |
-
return f"β **Server Error**\
|
| 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"β **
|
| 103 |
|
| 104 |
except requests.exceptions.Timeout:
|
| 105 |
-
return "β **Timeout
|
| 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"β **
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
def create_interface():
|
| 114 |
-
|
| 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 |
-
|
| 120 |
-
|
| 121 |
-
analytics_enabled=False
|
| 122 |
-
) as demo:
|
| 123 |
|
| 124 |
-
gr.
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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="
|
| 136 |
-
placeholder="Type your message
|
| 137 |
-
scale=4
|
| 138 |
-
lines=2
|
| 139 |
)
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 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 =
|
| 152 |
chat_history.append([message, bot_message])
|
| 153 |
return "", chat_history
|
| 154 |
-
|
| 155 |
# Event handlers
|
| 156 |
-
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 157 |
-
|
| 158 |
-
|
| 159 |
|
| 160 |
# Examples
|
| 161 |
gr.Examples(
|
| 162 |
examples=[
|
| 163 |
-
["Hello!
|
| 164 |
-
["Explain
|
| 165 |
-
["Write a Python function to
|
| 166 |
-
["What
|
| 167 |
-
["Help me debug this error: 'list index out of range'"]
|
| 168 |
],
|
| 169 |
inputs=msg
|
| 170 |
)
|
| 171 |
|
| 172 |
-
# Footer
|
| 173 |
gr.Markdown("""
|
| 174 |
---
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|