pytzen commited on
Commit
ad38076
1 Parent(s): da0e0c0

feature: chatbot

Browse files
Files changed (1) hide show
  1. app/main.py +10 -8
app/main.py CHANGED
@@ -1,17 +1,19 @@
1
  from fastapi import FastAPI
2
  from fastapi.responses import HTMLResponse
3
  from fastapi.staticfiles import StaticFiles
4
- from typing import Union
5
 
6
  app = FastAPI()
 
 
 
7
 
8
- app.mount("/static", StaticFiles(directory="app/static"), name="static")
9
-
10
- @app.get("/", response_class=HTMLResponse)
11
  def read_root():
12
- with open("app/index.html", "r") as html_file:
13
  return html_file.read()
14
 
15
- @app.get("/items/{item_id}")
16
- def read_item(item_id: int, q: Union[str, None] = None):
17
- return {"item_id": item_id, "q": q}
 
 
1
  from fastapi import FastAPI
2
  from fastapi.responses import HTMLResponse
3
  from fastapi.staticfiles import StaticFiles
4
+ from transformers import pipeline
5
 
6
  app = FastAPI()
7
+ app.mount('/static', StaticFiles(directory='app/static'), name='static')
8
+ question_answerer = pipeline(task='question-answering',
9
+ model='distilbert-base-cased-distilled-squad')
10
 
11
+ @app.get('/', response_class=HTMLResponse)
 
 
12
  def read_root():
13
+ with open('app/index.html', 'r') as html_file:
14
  return html_file.read()
15
 
16
+ @app.post('/chatbot')
17
+ async def ask_question(question: str, context: str):
18
+ result = question_answerer(question=question, context=context)
19
+ return {'answer': result['answer']}