Akwbw commited on
Commit
1ec79bd
·
verified ·
1 Parent(s): 26a40e4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+
4
+ # Apni API Key aur AgentRouter ka URL
5
+ API_KEY = "sk-ZCDGmT8bXQzwjzW2UeBYCAtlBtCSHJZ7flOUo6u6TEWOS7DA"
6
+ API_URL = "https://agentrouter.org/v1/chat/completions"
7
+
8
+ def chat_with_bot(message, history, model_name):
9
+ messages = []
10
+
11
+ # Purani chat history ko add karna
12
+ for user_msg, bot_msg in history:
13
+ messages.append({"role": "user", "content": user_msg})
14
+ messages.append({"role": "assistant", "content": bot_msg})
15
+
16
+ # Naya message add karna
17
+ messages.append({"role": "user", "content": message})
18
+
19
+ # 🔥 ALIBABA CLOUD WAF BYPASS HEADERS 🔥
20
+ # Yeh headers WAF ko bewaqoof banate hain ke request kisi real CLI tool se aa rahi hai
21
+ headers = {
22
+ "Content-Type": "application/json",
23
+ "Authorization": f"Bearer {API_KEY}",
24
+ "User-Agent": "codex_cli_rs/0.101.0 (Mac OS; arm64)",
25
+ "Originator": "codex_cli_rs",
26
+ "Accept": "application/json"
27
+ }
28
+
29
+ payload = {
30
+ "model": model_name,
31
+ "messages": messages
32
+ }
33
+
34
+ try:
35
+ response = requests.post(API_URL, headers=headers, json=payload)
36
+
37
+ # Check karna ke kahin Alibaba WAF ne phir bhi to block nahi kiya
38
+ if "CF_APP_WAF" in response.text or "<html" in response.text.lower():
39
+ yield "❌ **Alibaba Cloud WAF Block!**\nFirewall ne Hugging Face ki IP ko rok diya hai. Thori dair baad try karein."
40
+ return
41
+
42
+ data = response.json()
43
+
44
+ if response.status_code == 200:
45
+ yield data['choices'][0]['message']['content']
46
+ else:
47
+ yield f"⚠️ **API Error ({response.status_code}):** {data.get('error', {}).get('message', response.text)}"
48
+
49
+ except Exception as e:
50
+ yield f"🔌 **Connection Error:** {str(e)}"
51
+
52
+ # Gradio Interface (UI) Design karna
53
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
54
+ gr.Markdown("<center><h1>🤖 AgentRouter Chatbot</h1><p>Running with Alibaba WAF Bypass Headers</p></center>")
55
+
56
+ with gr.Row():
57
+ model_dropdown = gr.Dropdown(
58
+ choices=["deepseek-v4-pro", "deepseek-v4-flash", "claude-haiku-4-5-20251001", "claude-opus-4-6", "glm-5.1"],
59
+ value="deepseek-v4-pro",
60
+ label="Select AI Model"
61
+ )
62
+
63
+ chat = gr.ChatInterface(
64
+ fn=chat_with_bot,
65
+ additional_inputs=[model_dropdown]
66
+ )
67
+
68
+ # App ko run karna
69
+ if __name__ == "__main__":
70
+ demo.launch()