Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
|
@@ -6,7 +6,7 @@ from langchain.chains.summarize import load_summarize_chain
|
|
| 6 |
#https://python.langchain.com/docs/use_cases/summarization
|
| 7 |
from langchain import HuggingFaceHub
|
| 8 |
from huggingface_hub import InferenceClient
|
| 9 |
-
|
| 10 |
import requests
|
| 11 |
import sys
|
| 12 |
|
|
@@ -30,27 +30,24 @@ app = FastAPI()
|
|
| 30 |
@app.post('/')
|
| 31 |
async def home_api(request: Request):
|
| 32 |
data = await request.json()
|
| 33 |
-
|
| 34 |
-
print(
|
| 35 |
return {"Message": "FastAPI Home API Deploy Success on HF"}
|
| 36 |
|
| 37 |
@app.post('/api/chat')
|
| 38 |
async def chat(request: Request):
|
| 39 |
data = await request.json()
|
| 40 |
-
|
| 41 |
-
print(
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
raise HTTPException(status_code=400, detail=err_msg)
|
| 55 |
-
else:
|
| 56 |
-
raise HTTPException(status_code=400, detail="Invalid user question")
|
|
|
|
| 6 |
#https://python.langchain.com/docs/use_cases/summarization
|
| 7 |
from langchain import HuggingFaceHub
|
| 8 |
from huggingface_hub import InferenceClient
|
| 9 |
+
from bs4 import BeautifulSoup
|
| 10 |
import requests
|
| 11 |
import sys
|
| 12 |
|
|
|
|
| 30 |
@app.post('/')
|
| 31 |
async def home_api(request: Request):
|
| 32 |
data = await request.json()
|
| 33 |
+
target_url = data['target_url']
|
| 34 |
+
print(target_url)
|
| 35 |
return {"Message": "FastAPI Home API Deploy Success on HF"}
|
| 36 |
|
| 37 |
@app.post('/api/chat')
|
| 38 |
async def chat(request: Request):
|
| 39 |
data = await request.json()
|
| 40 |
+
target_url = data['target_url']
|
| 41 |
+
print(target_url)
|
| 42 |
+
try:
|
| 43 |
+
loader = WebBaseLoader(target_url)
|
| 44 |
+
print(target_url)
|
| 45 |
+
docs = loader.load()
|
| 46 |
+
result = chain.run(docs)
|
| 47 |
+
print("AI Summarization: " + result)
|
| 48 |
+
#return {'response': result} #FastAPI方式下,这个返回形式,有问题?
|
| 49 |
+
return JSONResponse({'response': result})
|
| 50 |
+
except Exception as e:
|
| 51 |
+
err_msg = "Wrong URL or URL not parsable."
|
| 52 |
+
print(err_msg)
|
| 53 |
+
raise HTTPException(status_code=400, detail=err_msg)
|
|
|
|
|
|
|
|
|