FENST4R commited on
Commit
c25af33
·
verified ·
1 Parent(s): 3c08ab3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +140 -55
app.py CHANGED
@@ -1,70 +1,155 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- def respond(
6
- message,
7
- history: list[dict[str, str]],
8
- system_message,
9
- max_tokens,
10
- temperature,
11
- top_p,
12
- hf_token: gr.OAuthToken,
13
- ):
14
- """
15
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
16
- """
17
- client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
 
 
 
 
18
 
19
- messages = [{"role": "system", "content": system_message}]
 
 
 
 
20
 
21
- messages.extend(history)
 
 
 
 
22
 
23
- messages.append({"role": "user", "content": message})
 
 
 
 
 
 
24
 
25
- response = ""
 
 
 
 
 
 
 
26
 
27
- for message in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
30
- stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
33
- ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = choices[0].delta.content
38
 
39
- response += token
40
- yield response
 
 
 
41
 
 
 
 
 
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- chatbot = gr.ChatInterface(
47
- respond,
48
- type="messages",
49
- additional_inputs=[
50
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
51
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
52
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
53
- gr.Slider(
54
- minimum=0.1,
55
- maximum=1.0,
56
- value=0.95,
57
- step=0.05,
58
- label="Top-p (nucleus sampling)",
59
- ),
60
- ],
61
- )
62
 
 
63
  with gr.Blocks() as demo:
64
- with gr.Sidebar():
65
- gr.LoginButton()
66
- chatbot.render()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
 
 
 
68
 
69
- if __name__ == "__main__":
70
- demo.launch()
 
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()