|
from fastapi import FastAPI, HTTPException |
|
from fastapi.responses import StreamingResponse, HTMLResponse |
|
import json |
|
from es_gpt import ESGPT |
|
import asyncio |
|
|
|
|
|
es = ESGPT(index_name="papers") |
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
|
|
|
|
@app.get("/", response_class=HTMLResponse) |
|
async def read_index(): |
|
with open("index.html", "r") as file: |
|
html = file.read() |
|
return html |
|
|
|
|
|
@app.get("/search") |
|
async def search(q: str): |
|
|
|
results = es.search(q) |
|
print(results) |
|
|
|
|
|
async def stream_response(): |
|
for hit in results: |
|
|
|
await asyncio.sleep(0.1) |
|
yield "data: " + json.dumps(hit) + "\n\n" |
|
yield "[DONE]" |
|
|
|
return StreamingResponse(stream_response(), media_type="text/event-stream") |
|
|
|
|
|
|
|
|
|
@app.get("/summary") |
|
async def summary(q: str): |
|
|
|
results = es.search(q) |
|
|
|
|
|
resp = es.summarize(q, results) |
|
|
|
if resp.status_code != 200: |
|
raise HTTPException(resp.status_code, resp.text) |
|
|
|
return StreamingResponse(resp.iter_content(1), |
|
media_type="text/event-stream") |
|
|