Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -56,3 +56,136 @@ data = {
|
|
| 56 |
]
|
| 57 |
}
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
]
|
| 57 |
}
|
| 58 |
|
| 59 |
+
|
| 60 |
+
import gradio as gr
|
| 61 |
+
import os
|
| 62 |
+
import requests
|
| 63 |
+
import json
|
| 64 |
+
|
| 65 |
+
# Daftar model yang tersedia
|
| 66 |
+
MODELS = [
|
| 67 |
+
"anthropic/claude-opus-4",
|
| 68 |
+
"anthropic/claude-sonnet-4",
|
| 69 |
+
"deepseek/deepseek-r1-0528",
|
| 70 |
+
"deepseek/deepseek-chat-v3-0324",
|
| 71 |
+
"openai/gpt-4.1",
|
| 72 |
+
"openai/gpt-4.1-mini",
|
| 73 |
+
"google/gemini-2.5-pro-preview",
|
| 74 |
+
"microsoft/phi-4-reasoning-plus",
|
| 75 |
+
"qwen/qwen3-30b-a3b",
|
| 76 |
+
"meta-llama/llama-guard-4-12b",
|
| 77 |
+
"meta-llama/llama-4-maverick",
|
| 78 |
+
"meta-llama/llama-4-scout",
|
| 79 |
+
"x-ai/grok-2-vision-1212"
|
| 80 |
+
]
|
| 81 |
+
|
| 82 |
+
def chat_with_openrouter(prompt, system_prompt, model):
|
| 83 |
+
try:
|
| 84 |
+
# Konfigurasi API OpenRouter
|
| 85 |
+
api_url = "https://openrouter.ai/api/v1/chat/completions"
|
| 86 |
+
api_key = os.environ.get("OPENROUTER_API_KEY")
|
| 87 |
+
|
| 88 |
+
# Header untuk autentikasi
|
| 89 |
+
headers = {
|
| 90 |
+
"Authorization": f"Bearer {api_key}",
|
| 91 |
+
"HTTP-Referer": "https://huggingface.co",
|
| 92 |
+
"X-Title": "OpenRouter Space",
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
# Bangun pesan dengan sistem prompt
|
| 96 |
+
messages = []
|
| 97 |
+
if system_prompt.strip():
|
| 98 |
+
messages.append({"role": "system", "content": system_prompt})
|
| 99 |
+
messages.append({"role": "user", "content": prompt})
|
| 100 |
+
|
| 101 |
+
# Body request
|
| 102 |
+
data = {
|
| 103 |
+
"model": model,
|
| 104 |
+
"messages": messages,
|
| 105 |
+
"max_tokens": 2048
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
# Kirim request ke OpenRouter
|
| 109 |
+
response = requests.post(api_url, headers=headers, json=data, timeout=90)
|
| 110 |
+
response.raise_for_status()
|
| 111 |
+
|
| 112 |
+
# Ekstrak dan kembalikan respons
|
| 113 |
+
result = response.json()
|
| 114 |
+
return result["choices"][0]["message"]["content"]
|
| 115 |
+
|
| 116 |
+
except Exception as e:
|
| 117 |
+
error_detail = f"{str(e)}"
|
| 118 |
+
if hasattr(e, 'response') and e.response:
|
| 119 |
+
try:
|
| 120 |
+
error_resp = e.response.json()
|
| 121 |
+
error_detail += f"\nError: {error_resp.get('error', {}).get('message', 'Unknown error')}"
|
| 122 |
+
except:
|
| 123 |
+
error_detail += f"\nStatus: {e.response.status_code}\nResponse: {e.response.text[:500]}"
|
| 124 |
+
return f"🚨 Error: {error_detail}"
|
| 125 |
+
|
| 126 |
+
# Buat antarmuka Gradio dengan lebih banyak kontrol
|
| 127 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 128 |
+
gr.Markdown("""
|
| 129 |
+
## 🤖 Advanced OpenRouter AI Assistant
|
| 130 |
+
Pilih model AI dan atur sistem prompt untuk mengontrol perilaku asisten
|
| 131 |
+
""")
|
| 132 |
+
|
| 133 |
+
with gr.Row():
|
| 134 |
+
with gr.Column(scale=1):
|
| 135 |
+
model_dropdown = gr.Dropdown(
|
| 136 |
+
label="Pilih Model AI",
|
| 137 |
+
choices=MODELS,
|
| 138 |
+
value="anthropic/claude-opus-4",
|
| 139 |
+
interactive=True
|
| 140 |
+
)
|
| 141 |
+
system_prompt = gr.Textbox(
|
| 142 |
+
label="Sistem Prompt",
|
| 143 |
+
placeholder="Contoh: 'Anda adalah ahli bahasa Indonesia yang ramah dan membantu'",
|
| 144 |
+
lines=4,
|
| 145 |
+
value="Anda adalah asisten AI yang membantu dengan jawaban informatif dan ramah. Gunakan bahasa Indonesia yang baik dan benar."
|
| 146 |
+
)
|
| 147 |
+
gr.Markdown("### Petunjuk:")
|
| 148 |
+
gr.Markdown("- Gunakan sistem prompt untuk mengatur perilaku AI")
|
| 149 |
+
gr.Markdown("- Model Claude Opus/Sonnet bagus untuk tugas kompleks")
|
| 150 |
+
gr.Markdown("- GPT-4.1 untuk kreativitas, Gemini untuk penalaran")
|
| 151 |
+
|
| 152 |
+
with gr.Column(scale=2):
|
| 153 |
+
inp = gr.Textbox(
|
| 154 |
+
label="Pertanyaan Anda",
|
| 155 |
+
placeholder="Tulis pertanyaan atau instruksi di sini...",
|
| 156 |
+
lines=5
|
| 157 |
+
)
|
| 158 |
+
out = gr.Textbox(
|
| 159 |
+
label="Jawaban AI",
|
| 160 |
+
lines=10,
|
| 161 |
+
interactive=False
|
| 162 |
+
)
|
| 163 |
+
btn = gr.Button("Kirim Permintaan", variant="primary")
|
| 164 |
+
|
| 165 |
+
# Contoh prompt
|
| 166 |
+
examples = gr.Examples(
|
| 167 |
+
examples=[
|
| 168 |
+
["Jelaskan teori relativitas khusus dengan analogi sederhana", "", "anthropic/claude-opus-4"],
|
| 169 |
+
["Buat puisi tentang teknologi AI dalam 4 bait", "Anda adalah penyair profesional", "openai/gpt-4.1"],
|
| 170 |
+
["Analisis dampak ekonomi AI di Indonesia tahun 2024", "Anda adalah analis ekonomi senior", "google/gemini-2.5-pro-preview"]
|
| 171 |
+
],
|
| 172 |
+
inputs=[inp, system_prompt, model_dropdown],
|
| 173 |
+
label="Contoh Permintaan"
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
btn.click(
|
| 177 |
+
fn=chat_with_openrouter,
|
| 178 |
+
inputs=[inp, system_prompt, model_dropdown],
|
| 179 |
+
outputs=out
|
| 180 |
+
)
|
| 181 |
+
|
| 182 |
+
# Footer dengan referensi
|
| 183 |
+
gr.Markdown("---")
|
| 184 |
+
gr.HTML("""
|
| 185 |
+
<div style="text-align: center">
|
| 186 |
+
<p>Powered by <a href="https://openrouter.ai" target="_blank">OpenRouter</a></p>
|
| 187 |
+
<img src="https://cdn.myportfolio.com/45200424-090f-478b-9be0-85ca9dee2b39/f3a8a7fb-b385-4a7a-8bee-e325c4b9cf9b_rw_600.png?h=fe0a7278654a29de048391005c6d748f" width="200">
|
| 188 |
+
</div>
|
| 189 |
+
""")
|
| 190 |
+
|
| 191 |
+
demo.launch()
|