Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,155 +1,116 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
| 3 |
|
| 4 |
-
# =====
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
"
|
| 12 |
-
"
|
| 13 |
-
"file": "https://fenst4r.life/api/file",
|
| 14 |
"stt": "http://neblora.fenst4r.life:5000/stt",
|
| 15 |
-
"
|
| 16 |
}
|
| 17 |
|
| 18 |
-
|
| 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 |
-
|
| 43 |
-
|
| 44 |
-
if
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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.
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
|
| 118 |
-
|
| 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 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
stt_output = gr.Textbox(label="Распознанный текст")
|
| 130 |
-
gr.Button("STT").click(stt_api, inputs=[stt_input], outputs=[stt_output])
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
gr.Button("Загрузить").click(upload_file, inputs=[file_input], outputs=[file_url])
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 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()
|