| import gradio as gr |
| import requests |
|
|
| |
| API_KEY = "sk-ZCDGmT8bXQzwjzW2UeBYCAtlBtCSHJZ7flOUo6u6TEWOS7DA" |
| API_URL = "https://agentrouter.org/v1/chat/completions" |
|
|
| def chat_with_bot(message, history, model_name): |
| messages = [] |
| |
| |
| for user_msg, bot_msg in history: |
| messages.append({"role": "user", "content": user_msg}) |
| messages.append({"role": "assistant", "content": bot_msg}) |
| |
| |
| messages.append({"role": "user", "content": message}) |
|
|
| |
| |
| headers = { |
| "Content-Type": "application/json", |
| "Authorization": f"Bearer {API_KEY}", |
| "User-Agent": "codex_cli_rs/0.101.0 (Mac OS; arm64)", |
| "Originator": "codex_cli_rs", |
| "Accept": "application/json" |
| } |
|
|
| payload = { |
| "model": model_name, |
| "messages": messages |
| } |
|
|
| try: |
| response = requests.post(API_URL, headers=headers, json=payload) |
| |
| |
| if "CF_APP_WAF" in response.text or "<html" in response.text.lower(): |
| yield "β **Alibaba Cloud WAF Block!**\nFirewall ne Hugging Face ki IP ko rok diya hai. Thori dair baad try karein." |
| return |
| |
| data = response.json() |
| |
| if response.status_code == 200: |
| yield data['choices'][0]['message']['content'] |
| else: |
| yield f"β οΈ **API Error ({response.status_code}):** {data.get('error', {}).get('message', response.text)}" |
| |
| except Exception as e: |
| yield f"π **Connection Error:** {str(e)}" |
|
|
| |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: |
| gr.Markdown("<center><h1>π€ AgentRouter Chatbot</h1><p>Running with Alibaba WAF Bypass Headers</p></center>") |
| |
| with gr.Row(): |
| model_dropdown = gr.Dropdown( |
| choices=["deepseek-v4-pro", "deepseek-v4-flash", "claude-haiku-4-5-20251001", "claude-opus-4-6", "glm-5.1"], |
| value="deepseek-v4-pro", |
| label="Select AI Model" |
| ) |
| |
| chat = gr.ChatInterface( |
| fn=chat_with_bot, |
| additional_inputs=[model_dropdown] |
| ) |
|
|
| |
| if __name__ == "__main__": |
| demo.launch() |