cody82 commited on
Commit
af4b77b
·
verified ·
1 Parent(s): f478cdc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -6
app.py CHANGED
@@ -6,8 +6,9 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
6
  import gradio as gr
7
  from fastapi import FastAPI
8
  from pydantic import BaseModel
 
9
 
10
- # Модель
11
  model_id = "sberbank-ai/rugpt3medium_based_on_gpt2"
12
  tokenizer = AutoTokenizer.from_pretrained(model_id)
13
  model = AutoModelForCausalLM.from_pretrained(model_id)
@@ -24,6 +25,7 @@ context = (
24
  def respond(message, history=None):
25
  prompt = f"Прочитай текст и ответь на вопрос:\n\n{context}\n\nВопрос: {message}\nОтвет:"
26
  input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device)
 
27
  with torch.no_grad():
28
  output_ids = model.generate(
29
  input_ids,
@@ -33,17 +35,20 @@ def respond(message, history=None):
33
  do_sample=True,
34
  pad_token_id=tokenizer.eos_token_id
35
  )
 
36
  output = tokenizer.decode(output_ids[0], skip_special_tokens=True)
 
37
  if "Ответ:" in output:
38
  answer = output.split("Ответ:")[-1].strip()
39
  else:
40
  answer = output[len(prompt):].strip()
 
41
  return answer
42
 
43
- # Gradio интерфейс
44
- chat = gr.ChatInterface(fn=respond, title="Иннополис Бот")
45
 
46
- # API
47
  app = FastAPI()
48
 
49
  class QuestionRequest(BaseModel):
@@ -53,5 +58,6 @@ class QuestionRequest(BaseModel):
53
  def ask(request: QuestionRequest):
54
  return {"answer": respond(request.question)}
55
 
56
- # Важно: экспорт для Hugging Face
57
- demo = gr.mount_gradio_app(app, chat, path="/")
 
 
6
  import gradio as gr
7
  from fastapi import FastAPI
8
  from pydantic import BaseModel
9
+ import uvicorn
10
 
11
+ # === Модель ===
12
  model_id = "sberbank-ai/rugpt3medium_based_on_gpt2"
13
  tokenizer = AutoTokenizer.from_pretrained(model_id)
14
  model = AutoModelForCausalLM.from_pretrained(model_id)
 
25
  def respond(message, history=None):
26
  prompt = f"Прочитай текст и ответь на вопрос:\n\n{context}\n\nВопрос: {message}\nОтвет:"
27
  input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device)
28
+
29
  with torch.no_grad():
30
  output_ids = model.generate(
31
  input_ids,
 
35
  do_sample=True,
36
  pad_token_id=tokenizer.eos_token_id
37
  )
38
+
39
  output = tokenizer.decode(output_ids[0], skip_special_tokens=True)
40
+
41
  if "Ответ:" in output:
42
  answer = output.split("Ответ:")[-1].strip()
43
  else:
44
  answer = output[len(prompt):].strip()
45
+
46
  return answer
47
 
48
+ # === Gradio интерфейс ===
49
+ chat_ui = gr.ChatInterface(fn=respond, title="Иннополис Бот")
50
 
51
+ # === FastAPI для API ===
52
  app = FastAPI()
53
 
54
  class QuestionRequest(BaseModel):
 
58
  def ask(request: QuestionRequest):
59
  return {"answer": respond(request.question)}
60
 
61
+ # === Mount Gradio UI на FastAPI ===
62
+ # Этот объект должен быть экспортирован как `demo`
63
+ demo = gr.mount_gradio_app(app, chat_ui, path="/")