Spaces:
Sleeping
Sleeping
File size: 1,644 Bytes
dc7f040 294c23c dc7f040 6d562d7 dc7f040 75968f7 6d562d7 dc7f040 13503fe 6d562d7 df93b25 6d562d7 df93b25 dc7f040 13503fe dc7f040 13503fe |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
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 будет конфликтовать
|