Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,185 +1,378 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
MODELS =
|
| 7 |
-
"anthropic/claude-opus-4"
|
| 8 |
-
"anthropic/claude-sonnet-4"
|
| 9 |
-
"deepseek/deepseek-r1-0528"
|
| 10 |
-
"deepseek/deepseek-chat-v3-0324"
|
| 11 |
-
"openai/gpt-4.1"
|
| 12 |
-
"openai/gpt-4.1-mini"
|
| 13 |
-
"google/gemini-2.5-pro-preview"
|
| 14 |
-
"microsoft/phi-4-reasoning-plus"
|
| 15 |
-
"qwen/qwen3-30b-a3b"
|
| 16 |
-
"meta-llama/llama-guard-4-12b"
|
| 17 |
-
"meta-llama/llama-4-maverick"
|
| 18 |
-
"meta-llama/llama-4-scout"
|
| 19 |
-
"x-ai/grok-2-vision-1212"
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
try:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
)
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
max_new_tokens=256,
|
| 35 |
-
temperature=0.7,
|
| 36 |
-
top_p=0.9
|
| 37 |
-
)
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
-
|
| 42 |
|
| 43 |
-
# CSS
|
| 44 |
custom_css = """
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
--text: #1f2937;
|
| 50 |
-
--border: #e5e7eb;
|
| 51 |
}
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
}
|
| 57 |
|
| 58 |
-
.
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
| 63 |
}
|
| 64 |
|
| 65 |
-
.
|
| 66 |
-
|
|
|
|
|
|
|
| 67 |
}
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
margin-bottom: 20px;
|
| 77 |
}
|
| 78 |
|
| 79 |
-
.
|
| 80 |
-
|
| 81 |
-
border-
|
| 82 |
-
|
|
|
|
|
|
|
| 83 |
}
|
| 84 |
|
| 85 |
-
.
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
| 88 |
}
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
| 95 |
}
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
cursor: pointer;
|
| 103 |
-
transition: all 0.2s;
|
| 104 |
-
font-weight: 500;
|
| 105 |
}
|
| 106 |
|
| 107 |
-
.
|
| 108 |
-
|
| 109 |
-
|
| 110 |
}
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
| 114 |
color: white;
|
| 115 |
-
|
| 116 |
-
|
| 117 |
}
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
margin-top: 20px;
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
}
|
| 125 |
"""
|
| 126 |
|
| 127 |
-
#
|
| 128 |
-
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
with gr.Row():
|
| 133 |
with gr.Column(scale=3):
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
label="Input Prompt",
|
| 141 |
-
placeholder="Ketik pertanyaan atau instruksi di sini...",
|
| 142 |
-
lines=5
|
| 143 |
)
|
|
|
|
| 144 |
with gr.Row():
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
-
with gr.Column(scale=
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
model_name.split("(")[0].strip(),
|
| 157 |
-
elem_classes="model-btn"
|
| 158 |
-
).click(
|
| 159 |
-
fn=lambda m=model_name: m,
|
| 160 |
-
inputs=[],
|
| 161 |
-
outputs=model_selector
|
| 162 |
)
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
# Event handlers
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
)
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
if __name__ == "__main__":
|
| 185 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
import os
|
| 5 |
+
from typing import Iterator
|
| 6 |
|
| 7 |
+
# Model list
|
| 8 |
+
MODELS = [
|
| 9 |
+
"anthropic/claude-opus-4",
|
| 10 |
+
"anthropic/claude-sonnet-4",
|
| 11 |
+
"deepseek/deepseek-r1-0528",
|
| 12 |
+
"deepseek/deepseek-chat-v3-0324",
|
| 13 |
+
"openai/gpt-4.1",
|
| 14 |
+
"openai/gpt-4.1-mini",
|
| 15 |
+
"google/gemini-2.5-pro-preview",
|
| 16 |
+
"microsoft/phi-4-reasoning-plus",
|
| 17 |
+
"qwen/qwen3-30b-a3b",
|
| 18 |
+
"meta-llama/llama-guard-4-12b",
|
| 19 |
+
"meta-llama/llama-4-maverick",
|
| 20 |
+
"meta-llama/llama-4-scout",
|
| 21 |
+
"x-ai/grok-2-vision-1212"
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
def get_openrouter_response(message: str, history: list, model: str, api_key: str) -> Iterator[str]:
|
| 25 |
+
"""Stream response from OpenRouter API"""
|
| 26 |
+
|
| 27 |
+
if not api_key:
|
| 28 |
+
yield "β Please enter your OpenRouter API key in the settings below."
|
| 29 |
+
return
|
| 30 |
+
|
| 31 |
+
# Prepare messages for API
|
| 32 |
+
messages = []
|
| 33 |
+
|
| 34 |
+
# Add conversation history
|
| 35 |
+
for user_msg, assistant_msg in history:
|
| 36 |
+
messages.append({"role": "user", "content": user_msg})
|
| 37 |
+
if assistant_msg:
|
| 38 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 39 |
+
|
| 40 |
+
# Add current message
|
| 41 |
+
messages.append({"role": "user", "content": message})
|
| 42 |
+
|
| 43 |
+
# API request
|
| 44 |
+
headers = {
|
| 45 |
+
"Authorization": f"Bearer {api_key}",
|
| 46 |
+
"Content-Type": "application/json",
|
| 47 |
+
"HTTP-Referer": "https://huggingface.co",
|
| 48 |
+
"X-Title": "HuggingFace OpenRouter Chat"
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
data = {
|
| 52 |
+
"model": model,
|
| 53 |
+
"messages": messages,
|
| 54 |
+
"stream": True
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
try:
|
| 58 |
+
response = requests.post(
|
| 59 |
+
"https://openrouter.ai/api/v1/chat/completions",
|
| 60 |
+
headers=headers,
|
| 61 |
+
json=data,
|
| 62 |
+
stream=True
|
| 63 |
)
|
| 64 |
|
| 65 |
+
if response.status_code != 200:
|
| 66 |
+
yield f"β Error: {response.status_code} - {response.text}"
|
| 67 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
# Stream the response
|
| 70 |
+
full_response = ""
|
| 71 |
+
for line in response.iter_lines():
|
| 72 |
+
if line:
|
| 73 |
+
line_text = line.decode('utf-8')
|
| 74 |
+
if line_text.startswith('data: '):
|
| 75 |
+
if line_text.strip() == 'data: [DONE]':
|
| 76 |
+
break
|
| 77 |
+
|
| 78 |
+
try:
|
| 79 |
+
json_data = json.loads(line_text[6:])
|
| 80 |
+
if 'choices' in json_data and len(json_data['choices']) > 0:
|
| 81 |
+
delta = json_data['choices'][0].get('delta', {})
|
| 82 |
+
if 'content' in delta:
|
| 83 |
+
content = delta['content']
|
| 84 |
+
full_response += content
|
| 85 |
+
yield full_response
|
| 86 |
+
except json.JSONDecodeError:
|
| 87 |
+
continue
|
| 88 |
+
|
| 89 |
except Exception as e:
|
| 90 |
+
yield f"β Error: {str(e)}"
|
| 91 |
|
| 92 |
+
# Custom CSS
|
| 93 |
custom_css = """
|
| 94 |
+
/* Modern Dark Theme */
|
| 95 |
+
.gradio-container {
|
| 96 |
+
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
|
| 97 |
+
font-family: 'Inter', 'Segoe UI', sans-serif;
|
|
|
|
|
|
|
| 98 |
}
|
| 99 |
|
| 100 |
+
/* Header Styling */
|
| 101 |
+
.main-header {
|
| 102 |
+
text-align: center;
|
| 103 |
+
padding: 2rem 0;
|
| 104 |
+
margin-bottom: 2rem;
|
| 105 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 106 |
+
border-radius: 15px;
|
| 107 |
+
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
|
| 108 |
}
|
| 109 |
|
| 110 |
+
.main-header h1 {
|
| 111 |
+
color: white;
|
| 112 |
+
font-size: 2.5rem;
|
| 113 |
+
font-weight: 700;
|
| 114 |
+
margin: 0;
|
| 115 |
+
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
|
| 116 |
}
|
| 117 |
|
| 118 |
+
.main-header p {
|
| 119 |
+
color: rgba(255, 255, 255, 0.9);
|
| 120 |
+
font-size: 1.1rem;
|
| 121 |
+
margin-top: 0.5rem;
|
| 122 |
}
|
| 123 |
|
| 124 |
+
/* Chat Interface */
|
| 125 |
+
#chatbot {
|
| 126 |
+
/* Changed from important declarations for better compatibility */
|
| 127 |
+
background-color: rgba(30, 30, 50, 0.8);
|
| 128 |
+
border-radius: 15px;
|
| 129 |
+
border: 1px solid rgba(255, 255, 255, 0.1);
|
| 130 |
+
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
|
|
|
|
| 131 |
}
|
| 132 |
|
| 133 |
+
#chatbot .user {
|
| 134 |
+
background-color: rgba(102, 126, 234, 0.2);
|
| 135 |
+
border-left: 4px solid #667eea;
|
| 136 |
+
border-radius: 10px;
|
| 137 |
+
padding: 10px;
|
| 138 |
+
margin: 5px;
|
| 139 |
}
|
| 140 |
|
| 141 |
+
#chatbot .bot {
|
| 142 |
+
background-color: rgba(118, 75, 162, 0.2);
|
| 143 |
+
border-left: 4px solid #764ba2;
|
| 144 |
+
border-radius: 10px;
|
| 145 |
+
padding: 10px;
|
| 146 |
+
margin: 5px;
|
| 147 |
}
|
| 148 |
|
| 149 |
+
/* Input Areas */
|
| 150 |
+
#msg-input {
|
| 151 |
+
background-color: rgba(30, 30, 50, 0.8);
|
| 152 |
+
border: 2px solid rgba(102, 126, 234, 0.5);
|
| 153 |
+
border-radius: 10px;
|
| 154 |
+
color: white;
|
| 155 |
+
font-size: 16px;
|
| 156 |
+
transition: all 0.3s ease;
|
| 157 |
}
|
| 158 |
|
| 159 |
+
#msg-input:focus {
|
| 160 |
+
border-color: #667eea;
|
| 161 |
+
box-shadow: 0 0 15px rgba(102, 126, 234, 0.3);
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
/* Buttons */
|
| 165 |
+
.submit-btn, .clear-btn, .settings-btn {
|
| 166 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 167 |
+
border: none;
|
| 168 |
+
border-radius: 10px;
|
| 169 |
+
color: white;
|
| 170 |
+
font-weight: 600;
|
| 171 |
+
padding: 12px 24px;
|
| 172 |
+
transition: all 0.3s ease;
|
| 173 |
cursor: pointer;
|
|
|
|
|
|
|
| 174 |
}
|
| 175 |
|
| 176 |
+
.submit-btn:hover, .clear-btn:hover, .settings-btn:hover {
|
| 177 |
+
transform: translateY(-2px);
|
| 178 |
+
box-shadow: 0 5px 20px rgba(102, 126, 234, 0.4);
|
| 179 |
}
|
| 180 |
|
| 181 |
+
/* Dropdown */
|
| 182 |
+
#model-dropdown {
|
| 183 |
+
background-color: rgba(30, 30, 50, 0.9);
|
| 184 |
+
border: 2px solid rgba(102, 126, 234, 0.5);
|
| 185 |
+
border-radius: 10px;
|
| 186 |
color: white;
|
| 187 |
+
padding: 10px;
|
| 188 |
+
font-size: 14px;
|
| 189 |
}
|
| 190 |
|
| 191 |
+
#model-dropdown:focus {
|
| 192 |
+
border-color: #667eea;
|
| 193 |
+
outline: none;
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
/* Settings Panel */
|
| 197 |
+
.settings-panel {
|
| 198 |
+
background: rgba(30, 30, 50, 0.8);
|
| 199 |
+
border-radius: 15px;
|
| 200 |
+
padding: 20px;
|
| 201 |
margin-top: 20px;
|
| 202 |
+
border: 1px solid rgba(255, 255, 255, 0.1);
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
/* API Key Input */
|
| 206 |
+
#api-key-input {
|
| 207 |
+
background-color: rgba(20, 20, 40, 0.8);
|
| 208 |
+
border: 2px solid rgba(102, 126, 234, 0.3);
|
| 209 |
+
border-radius: 10px;
|
| 210 |
+
color: white;
|
| 211 |
+
font-family: monospace;
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
/* Info Box */
|
| 215 |
+
.info-box {
|
| 216 |
+
background: rgba(102, 126, 234, 0.1);
|
| 217 |
+
border-left: 4px solid #667eea;
|
| 218 |
+
border-radius: 8px;
|
| 219 |
+
padding: 15px;
|
| 220 |
+
margin: 10px 0;
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
/* Model List */
|
| 224 |
+
.model-info {
|
| 225 |
+
background: rgba(30, 30, 50, 0.6);
|
| 226 |
+
border-radius: 10px;
|
| 227 |
+
padding: 15px;
|
| 228 |
+
margin: 10px 0;
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
/* Labels */
|
| 232 |
+
label {
|
| 233 |
+
color: rgba(255, 255, 255, 0.9);
|
| 234 |
+
font-weight: 600;
|
| 235 |
+
margin-bottom: 5px;
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
/* Dark theme for gradio components */
|
| 239 |
+
.dark {
|
| 240 |
+
--block-background-fill: rgba(30, 30, 50, 0.8);
|
| 241 |
+
--body-background-fill: transparent;
|
| 242 |
+
--color-accent: #667eea;
|
| 243 |
}
|
| 244 |
"""
|
| 245 |
|
| 246 |
+
# Create interface
|
| 247 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Soft(primary_hue="violet", neutral_hue="slate").set(
|
| 248 |
+
loader_color="#667eea",
|
| 249 |
+
slider_color="#667eea",
|
| 250 |
+
)) as demo:
|
| 251 |
+
|
| 252 |
+
# Header
|
| 253 |
+
gr.HTML("""
|
| 254 |
+
<div class="main-header">
|
| 255 |
+
<h1>π OpenRouter Multi-Model Chat</h1>
|
| 256 |
+
<p>Chat with multiple AI models through OpenRouter API</p>
|
| 257 |
+
</div>
|
| 258 |
+
""")
|
| 259 |
+
|
| 260 |
+
# State for API key
|
| 261 |
+
api_key_state = gr.State("")
|
| 262 |
|
| 263 |
with gr.Row():
|
| 264 |
with gr.Column(scale=3):
|
| 265 |
+
# Chat interface
|
| 266 |
+
chatbot = gr.Chatbot(
|
| 267 |
+
label="Chat",
|
| 268 |
+
elem_id="chatbot",
|
| 269 |
+
height=500,
|
| 270 |
+
bubble_full_width=False
|
|
|
|
|
|
|
|
|
|
| 271 |
)
|
| 272 |
+
|
| 273 |
with gr.Row():
|
| 274 |
+
msg = gr.Textbox(
|
| 275 |
+
label="Type your message",
|
| 276 |
+
placeholder="Ask me anything...",
|
| 277 |
+
lines=2,
|
| 278 |
+
max_lines=4,
|
| 279 |
+
elem_id="msg-input"
|
| 280 |
+
)
|
| 281 |
+
|
| 282 |
+
with gr.Row():
|
| 283 |
+
submit = gr.Button("π Send", variant="primary", elem_classes="submit-btn")
|
| 284 |
+
clear = gr.Button("ποΈ Clear", variant="secondary", elem_classes="clear-btn")
|
| 285 |
|
| 286 |
+
with gr.Column(scale=1):
|
| 287 |
+
# Model selection
|
| 288 |
+
model_dropdown = gr.Dropdown(
|
| 289 |
+
choices=MODELS,
|
| 290 |
+
value=MODELS[0],
|
| 291 |
+
label="π€ Select Model",
|
| 292 |
+
interactive=True,
|
| 293 |
+
elem_id="model-dropdown"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
)
|
| 295 |
+
|
| 296 |
+
gr.HTML("""
|
| 297 |
+
<div class="info-box">
|
| 298 |
+
<p><strong>Available Models:</strong></p>
|
| 299 |
+
<ul style="margin: 5px 0; padding-left: 20px;">
|
| 300 |
+
<li>Claude (Anthropic)</li>
|
| 301 |
+
<li>DeepSeek</li>
|
| 302 |
+
<li>GPT-4 (OpenAI)</li>
|
| 303 |
+
<li>Gemini (Google)</li>
|
| 304 |
+
<li>Phi-4 (Microsoft)</li>
|
| 305 |
+
<li>Qwen</li>
|
| 306 |
+
<li>LLaMA (Meta)</li>
|
| 307 |
+
<li>Grok (xAI)</li>
|
| 308 |
+
</ul>
|
| 309 |
+
</div>
|
| 310 |
+
""")
|
| 311 |
+
|
| 312 |
+
# Settings
|
| 313 |
+
with gr.Accordion("βοΈ Settings", open=False):
|
| 314 |
+
api_key_input = gr.Textbox(
|
| 315 |
+
label="OpenRouter API Key",
|
| 316 |
+
placeholder="sk-or-v1-...",
|
| 317 |
+
type="password",
|
| 318 |
+
elem_id="api-key-input"
|
| 319 |
+
)
|
| 320 |
+
|
| 321 |
+
gr.HTML("""
|
| 322 |
+
<div class="info-box">
|
| 323 |
+
<p>Get your API key from <a href="https://openrouter.ai/keys" target="_blank" style="color: #667eea;">OpenRouter</a></p>
|
| 324 |
+
</div>
|
| 325 |
+
""")
|
| 326 |
|
| 327 |
# Event handlers
|
| 328 |
+
def update_api_key(key):
|
| 329 |
+
return key
|
| 330 |
+
|
| 331 |
+
api_key_input.change(update_api_key, inputs=[api_key_input], outputs=[api_key_state])
|
| 332 |
+
|
| 333 |
+
def user_submit(message, history, model, api_key):
|
| 334 |
+
return "", history + [[message, None]]
|
| 335 |
+
|
| 336 |
+
def bot_response(history, model, api_key):
|
| 337 |
+
if not history:
|
| 338 |
+
return history
|
| 339 |
+
|
| 340 |
+
user_message = history[-1][0]
|
| 341 |
+
bot_message = ""
|
| 342 |
+
|
| 343 |
+
for chunk in get_openrouter_response(user_message, history[:-1], model, api_key):
|
| 344 |
+
bot_message = chunk
|
| 345 |
+
history[-1][1] = bot_message
|
| 346 |
+
yield history
|
| 347 |
+
|
| 348 |
+
msg.submit(
|
| 349 |
+
user_submit,
|
| 350 |
+
[msg, chatbot, model_dropdown, api_key_state],
|
| 351 |
+
[msg, chatbot]
|
| 352 |
+
).then(
|
| 353 |
+
bot_response,
|
| 354 |
+
[chatbot, model_dropdown, api_key_state],
|
| 355 |
+
chatbot
|
| 356 |
)
|
| 357 |
|
| 358 |
+
submit.click(
|
| 359 |
+
user_submit,
|
| 360 |
+
[msg, chatbot, model_dropdown, api_key_state],
|
| 361 |
+
[msg, chatbot]
|
| 362 |
+
).then(
|
| 363 |
+
bot_response,
|
| 364 |
+
[chatbot, model_dropdown, api_key_state],
|
| 365 |
+
chatbot
|
| 366 |
)
|
| 367 |
+
|
| 368 |
+
clear.click(lambda: None, None, chatbot)
|
| 369 |
+
|
| 370 |
+
# Footer
|
| 371 |
+
gr.HTML("""
|
| 372 |
+
<div style="text-align: center; margin-top: 30px; color: rgba(255, 255, 255, 0.6);">
|
| 373 |
+
<p>Built with AHMAD ANUGERAH using Gradio & OpenRouter</p>
|
| 374 |
+
</div>
|
| 375 |
+
""")
|
| 376 |
|
| 377 |
if __name__ == "__main__":
|
| 378 |
demo.launch()
|