|
|
import gradio as gr |
|
|
import requests |
|
|
import json |
|
|
import docx |
|
|
|
|
|
API_KEY = "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" |
|
|
|
|
|
|
|
|
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}" |
|
|
|
|
|
|
|
|
gr.Interface( |
|
|
fn=chat_with_ai, |
|
|
inputs=gr.Textbox(lines=3, placeholder="Введите вопрос..."), |
|
|
outputs="text", |
|
|
title="Чат с ИИ на основе документа", |
|
|
description="ИИ отвечает на вопросы по содержимому документа (.docx)" |
|
|
).launch() |
|
|
|