iShare commited on
Commit
a1461d3
·
1 Parent(s): 0e0f0fe

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +14 -6
main.py CHANGED
@@ -1,5 +1,3 @@
1
- import os
2
- from dotenv import load_dotenv
3
  from fastapi import FastAPI, Request, HTTPException
4
  from langchain.document_loaders import WebBaseLoader
5
  from langchain.chains.summarize import load_summarize_chain
@@ -9,8 +7,10 @@ from huggingface_hub import InferenceClient
9
  from bs4 import BeautifulSoup
10
  import requests
11
  import sys
12
-
 
13
  load_dotenv()
 
14
  hf_token = os.environ.get('HUGGINGFACEHUB_API_TOKEN')
15
  repo_id=os.environ.get('repo_id')
16
  #port = os.getenv('port')
@@ -26,6 +26,8 @@ llm = HuggingFaceHub(repo_id=repo_id, # for StarChat
26
  chain = load_summarize_chain(llm, chain_type="refine")
27
 
28
  app = FastAPI()
 
 
29
 
30
  @app.post('/')
31
  async def home_api(request: Request):
@@ -35,9 +37,15 @@ async def home_api(request: Request):
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)
 
 
 
1
  from fastapi import FastAPI, Request, HTTPException
2
  from langchain.document_loaders import WebBaseLoader
3
  from langchain.chains.summarize import load_summarize_chain
 
7
  from bs4 import BeautifulSoup
8
  import requests
9
  import sys
10
+ import os
11
+ from dotenv import load_dotenv
12
  load_dotenv()
13
+
14
  hf_token = os.environ.get('HUGGINGFACEHUB_API_TOKEN')
15
  repo_id=os.environ.get('repo_id')
16
  #port = os.getenv('port')
 
26
  chain = load_summarize_chain(llm, chain_type="refine")
27
 
28
  app = FastAPI()
29
+ class ChatRequest(BaseModel):
30
+ target_url: str
31
 
32
  @app.post('/')
33
  async def home_api(request: Request):
 
37
  return {"Message": "FastAPI Home API Deploy Success on HF"}
38
 
39
  @app.post('/api/chat')
40
+ async def chat(request: Request, chat_request: ChatRequest):
41
+ target_url = chat_request.target_url
42
+
43
+ #@app.post('/api/chat')
44
+ #async def chat(request: Request):
45
+ # data = await request.json()
46
+ # target_url = data['target_url']
47
+ #以上四行代码,可能有问题?出在data = await request.json()???理论上似乎也没有问题,因为后面的print(target_url),能够成功打印
48
+
49
  print(target_url)
50
  try:
51
  loader = WebBaseLoader(target_url)