Spaces:
Sleeping
Sleeping
import gradio as gr | |
import requests | |
import json | |
API_KEY = "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" | |
def chat_with_bot(user_input): | |
headers = { | |
"Authorization": f"Bearer {API_KEY}", | |
"Content-Type": "application/json", | |
} | |
payload = { | |
"model": "qwen/qwen3-14b:free", | |
"messages": [{"role": "user", "content": user_input}] | |
} | |
response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, data=json.dumps(payload)) | |
if response.status_code == 200: | |
try: | |
return response.json()["choices"][0]["message"]["content"] | |
except: | |
return "Ошибка обработки ответа" | |
else: | |
return f"Ошибка: {response.status_code} — {response.text}" | |
### Интерфейс для WEB (как раньше) | |
with gr.Blocks(css="style.css") as demo: | |
with gr.Column(elem_id="main-container"): | |
textbox = gr.Textbox(label="", placeholder="Введите ваш вопрос", lines=2) | |
output = gr.Textbox(label="", lines=6) | |
button = gr.Button("Отправить", elem_id="send-button") | |
button.click(chat_with_bot, inputs=textbox, outputs=output) | |
### ДОБАВЛЕНО: интерфейс для API (вызовов с React) | |
api = gr.Interface(fn=chat_with_bot, inputs=gr.Textbox(), outputs=gr.Textbox()) | |
### Запуск обоих интерфейсов | |
demo.launch() | |
# запускаем и demo, и API | |
api.launch(share=False, inline=False) # обязательно `inline=False` иначе web UI будет конфликтовать | |