FENST4R commited on
Commit
058d6f0
·
verified ·
1 Parent(s): c25af33

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -132
app.py CHANGED
@@ -1,155 +1,116 @@
1
  import gradio as gr
2
  import requests
 
3
 
4
- # ===== URL API =====
5
- URLS = {
 
 
 
 
 
 
 
 
6
  "chatgpt": "http://neblora.fenst4r.life:5400/gpt",
7
  "llama": "http://neblora.fenst4r.life:5500/llama",
8
- "analyze_image": "http://neblora.fenst4r.life:4848/analyze-image",
9
  "draw": "http://neblora.fenst4r.life:4000/draw",
10
  "v2draw": "http://neblora.fenst4r.life:4000/v2draw",
11
- "v3draw": "http://neblora.fenst4r.life:4000/v3draw",
12
- "say": "http://neblora.fenst4r.life:4000/say",
13
- "file": "https://fenst4r.life/api/file",
14
  "stt": "http://neblora.fenst4r.life:5000/stt",
15
- "tts": "http://neblora.fenst4r.life:4949/tts"
16
  }
17
 
18
- # ===== Функции API =====
19
- def chat_api(text, model="GPT-4", tts=False):
20
- payload = {"text": text, "model": model}
21
- r = requests.post(URLS["chatgpt"], json=payload)
22
- if r.status_code != 200:
23
- return f"Ошибка: {r.status_code}", None
24
- answer = r.json().get("response", "")
25
-
26
- audio_file = None
27
- if tts:
28
- tts_payload = {"text": answer, "voice":"nova"}
29
- tts_r = requests.post(URLS["tts"], json=tts_payload)
30
- if tts_r.status_code == 200:
31
- audio_file = "output.mp3"
32
- with open(audio_file, "wb") as f:
33
- f.write(tts_r.content)
34
- return answer, audio_file
35
-
36
- def llama_api(text):
37
- r = requests.post(URLS["llama"], json={"text": text})
38
- if r.status_code != 200:
39
- return f"Ошибка: {r.status_code}"
40
- return r.json().get("response", "")
41
 
42
- def analyze_image_api(image):
43
- r = requests.post(URLS["analyze_image"], files={"file": image})
44
- if r.status_code != 200:
45
- return f"Ошибка: {r.status_code}"
46
- return r.json().get("description", "")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
- def draw_api(prompt, size="512x512"):
49
- r = requests.post(URLS["draw"], json={"prompt": prompt, "size": size})
50
- if r.status_code != 200:
51
- return None
52
- with open("draw_result.png", "wb") as f:
53
- f.write(r.content)
54
- return "draw_result.png"
 
 
 
 
 
 
55
 
56
- def v2draw_api(prompt, format="json"):
57
- r = requests.post(URLS["v2draw"], json={"prompt": prompt, "format": format})
58
- if r.status_code != 200:
59
- return "Ошибка"
60
- if format=="json":
61
- data = r.json()
62
- return [img for img in data.get("images", [])]
63
- return r.content
 
 
 
 
64
 
65
- def v3draw_api(prompt, width=1280, height=1280, steps=20, scale=7.5, format="file"):
66
- payload = {"prompt": prompt, "width": width, "height": height, "steps": steps, "scale": scale, "format": format}
67
- r = requests.post(URLS["v3draw"], json=payload)
68
- if r.status_code != 200:
69
- return None
70
- if format=="json":
71
- return r.json().get("image")
72
- with open("v3draw.png", "wb") as f:
73
- f.write(r.content)
74
- return "v3draw.png"
75
-
76
- def say_api(text):
77
- r = requests.post(URLS["say"], json={"text": text})
78
- if r.status_code != 200:
79
- return f"Ошибка: {r.status_code}"
80
- return r.json().get("text", "")
81
-
82
- def stt_api(audio_file):
83
- r = requests.post(URLS["stt"], files={"file": audio_file})
84
- if r.status_code != 200:
85
- return f"Ошибка: {r.status_code}"
86
- return r.json().get("text", "")
87
-
88
- def upload_file(files):
89
- payload = {}
90
- for file in files:
91
- payload[file.name] = file.read().decode('utf-8')
92
- r = requests.post(URLS["file"], json=payload)
93
- if r.status_code != 200:
94
- return f"Ошибка: {r.status_code}"
95
- return r.json().get("url", "")
96
-
97
- # ===== Интерфейс Gradio =====
98
  with gr.Blocks() as demo:
99
- gr.Markdown("# NEIROST4R API Interface")
100
- with gr.Tab("ChatGPT / TTS"):
101
- text_input = gr.Textbox(label="Введите текст")
102
- model_choice = gr.Radio(choices=["GPT-4", "GPT-5"], value="GPT-4", label="Модель")
103
- tts_toggle = gr.Checkbox(label="Озвучить ответ")
104
- chat_output = gr.Textbox(label="Ответ ChatGPT")
105
- audio_output = gr.Audio(label="TTS", type="filepath")
106
- gr.Button("Отправить").click(chat_api, inputs=[text_input, model_choice, tts_toggle], outputs=[chat_output, audio_output])
107
 
108
- with gr.Tab("LLaMA 3.1"):
109
- llama_input = gr.Textbox(label="Введите текст")
110
- llama_output = gr.Textbox(label="Ответ LLaMA")
111
- gr.Button("Отправить").click(llama_api, inputs=[llama_input], outputs=[llama_output])
 
 
112
 
113
- with gr.Tab("Анализ изображения"):
114
- image_input = gr.File(label="Выберите изображение", file_types=[".jpg", ".png", ".webp"])
115
- image_desc = gr.Textbox(label="Описание изображения")
116
- gr.Button("Анализировать").click(analyze_image_api, inputs=[image_input], outputs=[image_desc])
117
 
118
- with gr.Tab("Draw / v2draw / v3draw"):
119
- prompt_input = gr.Textbox(label="Описание сцены")
120
- draw_out = gr.Image(label="Результат Draw")
121
- gr.Button("Draw").click(draw_api, inputs=[prompt_input], outputs=[draw_out])
122
 
123
- with gr.Tab("Say / STT"):
124
- say_input = gr.Textbox(label="Введите текст")
125
- say_output = gr.Textbox(label="Текстовое преобразование")
126
- gr.Button("Say").click(say_api, inputs=[say_input], outputs=[say_output])
127
-
128
- stt_input = gr.File(label="Аудиофайл")
129
- stt_output = gr.Textbox(label="Распознанный текст")
130
- gr.Button("STT").click(stt_api, inputs=[stt_input], outputs=[stt_output])
131
 
132
- with gr.Tab("Файлы"):
133
- file_input = gr.File(label="Загрузите файлы", file_types=[".txt", ".json"], file_types_allow_multiple=True)
134
- file_url = gr.Textbox(label="Ссылка на файлы")
135
- gr.Button("Загрузить").click(upload_file, inputs=[file_input], outputs=[file_url])
136
 
137
- with gr.Tab("Документация"):
138
- gr.Markdown("""
139
- ## Содержание
140
- - Инструкция по API NEIROST4R
141
- - Быстрый старт
142
- - API версии 1-5
143
- - Python примеры
144
- - Библиотека neirost4r
145
- - Профили
146
- - Флаги
147
- - Примеры
148
- - Провайдеры и модели
149
- - Обработка ошибок
150
-
151
- Новости в канале PUR🩷 CHANNEL: https://t.me/pur_channel
152
- Поддержка и баги: @error_kill
153
- """)
154
 
155
  demo.launch()
 
1
  import gradio as gr
2
  import requests
3
+ import json
4
 
5
+ # ====== API BASE URL ======
6
+ NEIROST4R_BASE = {
7
+ "ai_v1": "https://fenst4r.life/api/ai",
8
+ "ai_v2": "https://fenst4r.life/api/ai_v2",
9
+ "ai_v3": "https://fenst4r.life/api/ai_v3",
10
+ "ai_v4": "http://fenst4r.life/api/ai_v4",
11
+ "ai_v5": "http://fenst4r.life/api/ai_v5"
12
+ }
13
+
14
+ NEBLORA_BASE = {
15
  "chatgpt": "http://neblora.fenst4r.life:5400/gpt",
16
  "llama": "http://neblora.fenst4r.life:5500/llama",
 
17
  "draw": "http://neblora.fenst4r.life:4000/draw",
18
  "v2draw": "http://neblora.fenst4r.life:4000/v2draw",
19
+ "analyze_image": "http://neblora.fenst4r.life:4848/analyze-image",
20
+ "tts": "http://neblora.fenst4r.life:4949/tts",
 
21
  "stt": "http://neblora.fenst4r.life:5000/stt",
22
+ "file": "https://fenst4r.life/api/file"
23
  }
24
 
25
+ HEADERS_JSON = {"Content-Type": "application/json"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
+ # ====== NEIROST4R CALL ======
28
+ def call_neirost4r(api_version, message, profile="friendly", flags=None):
29
+ if flags is None:
30
+ flags = {}
31
+ url = NEIROST4R_BASE.get(api_version)
32
+ if not url:
33
+ return "Unknown NEIROST4R API version"
34
+ headers = HEADERS_JSON.copy()
35
+ # v4 и v5 требуют User-Agent
36
+ if api_version in ["ai_v4", "ai_v5"]:
37
+ headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
38
+ data = {"message": message, "profile": profile}
39
+ if api_version in ["ai_v2", "ai_v3", "ai_v4", "ai_v5"]:
40
+ data["flags"] = flags
41
+ try:
42
+ response = requests.post(url, headers=headers, data=json.dumps(data), timeout=30)
43
+ return response.json()
44
+ except Exception as e:
45
+ return {"error": str(e)}
46
 
47
+ # ====== NEBLORA CALL ======
48
+ def call_neblora(endpoint, text, model=None):
49
+ url = NEBLORA_BASE.get(endpoint)
50
+ if not url:
51
+ return "Unknown Neblora endpoint"
52
+ data = {"text": text}
53
+ if model:
54
+ data["model"] = model
55
+ try:
56
+ response = requests.post(url, headers=HEADERS_JSON, data=json.dumps(data), timeout=30)
57
+ return response.json()
58
+ except Exception as e:
59
+ return {"error": str(e)}
60
 
61
+ # ====== Gradio Interface ======
62
+ def generate_response(api_type, endpoint, text, profile, model, flags_json):
63
+ flags = {}
64
+ if flags_json.strip():
65
+ try:
66
+ flags = json.loads(flags_json)
67
+ except:
68
+ return "Flags must be valid JSON"
69
+ if api_type == "NEIROST4R":
70
+ return call_neirost4r(endpoint, text, profile=profile, flags=flags)
71
+ else:
72
+ return call_neblora(endpoint, text, model=model)
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  with gr.Blocks() as demo:
75
+ gr.Markdown("## NEIROST4R + Neblora Unified Interface")
 
 
 
 
 
 
 
76
 
77
+ with gr.Row():
78
+ api_type = gr.Radio(["NEIROST4R", "Neblora"], label="Выберите API")
79
+ endpoint = gr.Dropdown(choices=[
80
+ "ai_v1","ai_v2","ai_v3","ai_v4","ai_v5",
81
+ "chatgpt","llama","draw","v2draw","analyze_image","tts","stt","file"
82
+ ], label="Endpoint / Модель")
83
 
84
+ text_input = gr.Textbox(label="Введите текст", lines=4)
85
+ profile_input = gr.Textbox(label="Profile (для NEIROST4R)", value="friendly")
86
+ model_input = gr.Textbox(label="Model (для Neblora, если нужно)", value="")
87
+ flags_input = gr.Textbox(label="Flags JSON (для NEIROST4R)", value="{}", lines=4)
88
 
89
+ output = gr.JSON(label="Ответ")
 
 
 
90
 
91
+ submit_btn = gr.Button("Отправить")
92
+ submit_btn.click(
93
+ generate_response,
94
+ inputs=[api_type, endpoint, text_input, profile_input, model_input, flags_input],
95
+ outputs=output
96
+ )
 
 
97
 
98
+ # ====== Файлы ======
99
+ file_input = gr.File(label="Загрузите файлы", file_types=[".txt", ".json"])
100
+ file_url = gr.Textbox(label="Ссылка на загруженный файл")
 
101
 
102
+ def upload_file(file):
103
+ if not file:
104
+ return ет файла"
105
+ try:
106
+ with open(file.name, "rb") as f:
107
+ files = {"file": f}
108
+ resp = requests.post(NEBLORA_BASE["file"], files=files)
109
+ return resp.json()
110
+ except Exception as e:
111
+ return {"error": str(e)}
112
+
113
+ file_btn = gr.Button("Загрузить")
114
+ file_btn.click(upload_file, inputs=file_input, outputs=file_url)
 
 
 
 
115
 
116
  demo.launch()