Opera8 commited on
Commit
18a35d1
·
verified ·
1 Parent(s): 755230c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -0
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import httpx
3
+ import json
4
+ import base64
5
+
6
+ # آدرس سرور رانفلر شما
7
+ URL = "https://ttsalpha.runflare.run/api/chat_proxy"
8
+
9
+ def convert_to_b64(path):
10
+ """تبدیل تصویر به فرمت Base64 خام بدون هدر آغازین"""
11
+ if not path:
12
+ return None
13
+ try:
14
+ with open(path, "rb") as image_file:
15
+ return base64.b64encode(image_file.read()).decode('utf-8')
16
+ except Exception as e:
17
+ print(f"Error encoding image: {e}")
18
+ return None
19
+
20
+ def respond(message, history):
21
+ """دریافت ورودی مالتی‌مودال از گرادیو و ارسال به API رانفلر"""
22
+ text_content = message.get("text", "")
23
+ files = message.get("files", [])
24
+
25
+ # بررسی وجود تصویر در پیام
26
+ image_path = None
27
+ if files:
28
+ # انتخاب اولین فایل آپلود شده
29
+ image_path = files[0]["path"] if isinstance(files[0], dict) else files[0]
30
+
31
+ img_b64 = convert_to_b64(image_path) if image_path else None
32
+
33
+ # ساخت لود درخواستی دقیقا مطابق مستندات شما
34
+ payload = {
35
+ "type": "text",
36
+ "content": text_content,
37
+ "model": "gpt5",
38
+ "text_history": "",
39
+ "image_base64": img_b64,
40
+ "session_id": "hf_space_test_session"
41
+ }
42
+
43
+ headers = {"Content-Type": "application/json"}
44
+ full_text = ""
45
+
46
+ try:
47
+ # برقراری ارتباط استریم با سرور رانفلر
48
+ with httpx.stream("POST", URL, json=payload, headers=headers, timeout=120.0) as r:
49
+ if r.status_code != 200:
50
+ yield f"❌ خطای سرور رانفلر: کد وضعیت {r.status_code}\nاحتمالاً سرور مقصد درخواست‌های خارج از کشور را مسدود کرده است."
51
+ return
52
+
53
+ for line in r.iter_lines():
54
+ if line.strip():
55
+ try:
56
+ data = json.loads(line)
57
+ if data.get("status") == "streaming" and "text" in data:
58
+ full_text += data["text"]
59
+ yield full_text
60
+ except json.JSONDecodeError:
61
+ pass
62
+
63
+ except httpx.ConnectError:
64
+ yield "❌ خطای اتصال (ConnectError): سرور Runflare از این موقعیت جغرافیایی (آمریکا/اروپا) در دسترس نیست."
65
+ except httpx.TimeoutException:
66
+ yield "⏳ خطای زمان پاسخ‌دهی (Timeout): سرور مقصد پاسخگو نبود."
67
+ except Exception as e:
68
+ yield f"❌ خطای غیرمنتظره: {e}"
69
+
70
+ # ساخت دمو با واسط کاربری متناسب با آخرین نسخه گرادیو
71
+ demo = gr.ChatInterface(
72
+ fn=respond,
73
+ title="Alpha AI Test Space 🤖",
74
+ description="تست اتصال متنی و تصویری به هوش مصنوعی آلفا از سرورهای خارج از کشور (Hugging Face / USA)",
75
+ multimodal=True,
76
+ textbox=gr.MultimodalTextbox(
77
+ placeholder="پیام خود را بنویسید یا تصویر آپلود کنید...",
78
+ file_types=["image"]
79
+ ),
80
+ examples=[
81
+ {"text": "درباره اهمیت هوش مصنوعی در زندگی روزمره توضیح بده.", "files": []},
82
+ {"text": "سه کتاب معروف در زمینه خودشناسی معرفی کن.", "files": []}
83
+ ],
84
+ theme="soft"
85
+ )
86
+
87
+ if __name__ == "__main__":
88
+ demo.launch()