Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import random
|
3 |
+
from typing import List, Tuple, Union
|
4 |
+
from gradio_client import Client
|
5 |
+
|
6 |
+
# Устанавливаем необходимые библиотеки
|
7 |
+
os.system("pip install gradio_client")
|
8 |
+
|
9 |
+
# Получаем список ключей из переменной окружения
|
10 |
+
api_keys = os.getenv("KEYS", "").split(",")
|
11 |
+
if not api_keys:
|
12 |
+
raise ValueError("API keys are not set in the environment variable KEYS")
|
13 |
+
|
14 |
+
# Выбираем случайный ключ для каждого запроса
|
15 |
+
def get_random_api_key():
|
16 |
+
return random.choice(api_keys)
|
17 |
+
|
18 |
+
# Функция для вызова API /add_text
|
19 |
+
def add_text(_input: dict, _chatbot: List[Tuple[dict, dict]]) -> Tuple[dict, List[Tuple[dict, dict]]]:
|
20 |
+
api_key = get_random_api_key()
|
21 |
+
client = Client("Qwen/Qwen2.5-Turbo-1M-Demo", hf_token=api_key)
|
22 |
+
result = client.predict(
|
23 |
+
_input=_input,
|
24 |
+
_chatbot=_chatbot,
|
25 |
+
api_name="/add_text"
|
26 |
+
)
|
27 |
+
return result
|
28 |
+
|
29 |
+
# Функция для вызова API /agent_run
|
30 |
+
def agent_run(_chatbot: List[Tuple[dict, dict]]) -> List[Tuple[dict, dict]]:
|
31 |
+
api_key = get_random_api_key()
|
32 |
+
client = Client("Qwen/Qwen2.5-Turbo-1M-Demo", hf_token=api_key)
|
33 |
+
result = client.predict(
|
34 |
+
_chatbot=_chatbot,
|
35 |
+
api_name="/agent_run"
|
36 |
+
)
|
37 |
+
return result
|
38 |
+
|
39 |
+
# Функция для вызова API /flushed
|
40 |
+
def flushed() -> dict:
|
41 |
+
api_key = get_random_api_key()
|
42 |
+
client = Client("Qwen/Qwen2.5-Turbo-1M-Demo", hf_token=api_key)
|
43 |
+
result = client.predict(
|
44 |
+
api_name="/flushed"
|
45 |
+
)
|
46 |
+
return result
|
47 |
+
|
48 |
+
# Основная функция приложения
|
49 |
+
def app_gui():
|
50 |
+
import gradio as gr
|
51 |
+
|
52 |
+
def chat(message, chat_history):
|
53 |
+
# Добавляем текст в чат
|
54 |
+
_input = {"files": [], "text": message}
|
55 |
+
_chatbot = chat_history if chat_history else []
|
56 |
+
response, _chatbot = add_text(_input, _chatbot)
|
57 |
+
return "", _chatbot
|
58 |
+
|
59 |
+
def clear_chat():
|
60 |
+
# Очищаем чат
|
61 |
+
_chatbot = flushed()
|
62 |
+
return _chatbot
|
63 |
+
|
64 |
+
# Создаем интерфейс Gradio
|
65 |
+
with gr.Blocks() as demo:
|
66 |
+
chatbot = gr.Chatbot(label="Chatbot")
|
67 |
+
msg = gr.Textbox(label="Message")
|
68 |
+
clear = gr.Button("Clear")
|
69 |
+
|
70 |
+
msg.submit(chat, [msg, chatbot], [msg, chatbot])
|
71 |
+
clear.click(clear_chat, None, chatbot)
|
72 |
+
|
73 |
+
demo.launch()
|
74 |
+
|
75 |
+
if __name__ == '__main__':
|
76 |
+
app_gui()
|
77 |
+
|