File size: 1,610 Bytes
6c22961 17c7931 5049f49 17c7931 |
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 46 47 48 49 50 |
import gradio as gr
import requests
import json
import docx
API_KEY = "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"
# Чтение содержимого DOCX
def read_docx(file_path):
doc = docx.Document(file_path)
full_text = []
for para in doc.paragraphs:
full_text.append(para.text)
return '\n'.join(full_text)
# Загрузи и сохрани текст из документа
document_text = read_docx("yourfile.docx")[:300]
# Основная функция общения
def chat_with_ai(user_message):
prompt = f"Вот информация из документа:\n{document_text}\n\nВопрос: {user_message}"
response = requests.post(
url="https://openrouter.ai/api/v1/chat/completions",
headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
},
data=json.dumps({
"model": "qwen/qwen3-235b-a22b:free",
"messages": [
{"role": "user", "content": prompt}
]
})
)
if response.status_code == 200:
return response.json()["choices"][0]["message"]["content"]
else:
return f"Ошибка: {response.text}"
# Интерфейс Gradio
gr.Interface(
fn=chat_with_ai,
inputs=gr.Textbox(lines=3, placeholder="Введите вопрос..."),
outputs="text",
title="Чат с ИИ на основе документа",
description="ИИ отвечает на вопросы по содержимому документа (.docx)"
).launch()
|