Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,10 +2,8 @@ import os
|
|
2 |
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "0" # отключаем нестабильную загрузку
|
3 |
|
4 |
import torch
|
|
|
5 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
6 |
-
from fastapi import FastAPI
|
7 |
-
from fastapi.middleware.cors import CORSMiddleware
|
8 |
-
from pydantic import BaseModel
|
9 |
|
10 |
model_id = "sberbank-ai/rugpt3medium_based_on_gpt2"
|
11 |
|
@@ -21,8 +19,9 @@ context = (
|
|
21 |
"расположенный в городе Иннополис, Татарстан.\n"
|
22 |
)
|
23 |
|
24 |
-
def respond(message
|
25 |
prompt = f"Прочитай текст и ответь на вопрос:\n\n{context}\n\nВопрос: {message}\nОтвет:"
|
|
|
26 |
input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device)
|
27 |
|
28 |
with torch.no_grad():
|
@@ -37,6 +36,7 @@ def respond(message: str) -> str:
|
|
37 |
|
38 |
full_output = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
39 |
|
|
|
40 |
if "Ответ:" in full_output:
|
41 |
answer = full_output.split("Ответ:")[-1].strip()
|
42 |
else:
|
@@ -44,27 +44,12 @@ def respond(message: str) -> str:
|
|
44 |
|
45 |
return answer
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
allow_credentials=True,
|
53 |
-
allow_methods=["*"],
|
54 |
-
allow_headers=["*"],
|
55 |
)
|
56 |
|
57 |
-
class QuestionRequest(BaseModel):
|
58 |
-
question: str
|
59 |
-
|
60 |
-
class AnswerResponse(BaseModel):
|
61 |
-
answer: str
|
62 |
-
|
63 |
-
@app.post("/api/ask", response_model=AnswerResponse)
|
64 |
-
def ask_question(request: QuestionRequest):
|
65 |
-
answer = respond(request.question)
|
66 |
-
return {"answer": answer}
|
67 |
-
|
68 |
if __name__ == "__main__":
|
69 |
-
|
70 |
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
2 |
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "0" # отключаем нестабильную загрузку
|
3 |
|
4 |
import torch
|
5 |
+
import gradio as gr
|
6 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
|
|
|
7 |
|
8 |
model_id = "sberbank-ai/rugpt3medium_based_on_gpt2"
|
9 |
|
|
|
19 |
"расположенный в городе Иннополис, Татарстан.\n"
|
20 |
)
|
21 |
|
22 |
+
def respond(message, history=None):
|
23 |
prompt = f"Прочитай текст и ответь на вопрос:\n\n{context}\n\nВопрос: {message}\nОтвет:"
|
24 |
+
|
25 |
input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device)
|
26 |
|
27 |
with torch.no_grad():
|
|
|
36 |
|
37 |
full_output = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
38 |
|
39 |
+
# Извлекаем только текст после "Ответ:"
|
40 |
if "Ответ:" in full_output:
|
41 |
answer = full_output.split("Ответ:")[-1].strip()
|
42 |
else:
|
|
|
44 |
|
45 |
return answer
|
46 |
|
47 |
+
iface = gr.ChatInterface(
|
48 |
+
fn=respond,
|
49 |
+
title="Бот об Университете Иннополис (на русском)",
|
50 |
+
chatbot=gr.Chatbot(label="Диалог"),
|
51 |
+
textbox=gr.Textbox(placeholder="Задай вопрос на русском...", label="Твой вопрос")
|
|
|
|
|
|
|
52 |
)
|
53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
if __name__ == "__main__":
|
55 |
+
iface.launch()
|
|