binqiangliu commited on
Commit
1d1683c
·
1 Parent(s): 99f5f75

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -3
app.py CHANGED
@@ -2,6 +2,8 @@ import streamlit as st
2
  import requests
3
  import timeit
4
  import datetime
 
 
5
 
6
  st.set_page_config(page_title="AI Chat Client", layout="wide")
7
  st.title("AI Chat Client")
@@ -10,8 +12,7 @@ current_datetime_0= datetime.datetime.now()
10
  print(f"Anything happens, this ST app will execute from top down. @ {current_datetime_0}")
11
  print()
12
 
13
- # FastAPI 服务器的 URL
14
- fastapi_server_url = "https://binqiangliu-wechatarticleloaderfastapi.hf.space/get_ai_response"
15
 
16
  # 用户输入
17
  url = st.text_input("Enter the URL to chat with:")
@@ -28,13 +29,21 @@ if st.button('Get AI Response'):
28
  print()
29
  with st.spinner('Fetching AI response...'):
30
  # 构造请求
 
 
 
 
 
 
 
31
  data = {"url": url, "question": question}
32
  # 发送请求到 FastAPI 服务器
33
  current_datetime_0 = datetime.datetime.now()
34
  print(f'API调用请求发送开始 @ {current_datetime_0}')
35
  print()
36
  start_1 = timeit.default_timer() # Start timer
37
- response = requests.post(fastapi_server_url, json=data)
 
38
  end_1 = timeit.default_timer() # Start timer
39
  print(f'API调用请求发送结束,共耗时: @ {end_1 - start_1}')
40
  print()
 
2
  import requests
3
  import timeit
4
  import datetime
5
+ from dotenv import load_dotenv
6
+ load_dotenv()
7
 
8
  st.set_page_config(page_title="AI Chat Client", layout="wide")
9
  st.title("AI Chat Client")
 
12
  print(f"Anything happens, this ST app will execute from top down. @ {current_datetime_0}")
13
  print()
14
 
15
+ HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
 
16
 
17
  # 用户输入
18
  url = st.text_input("Enter the URL to chat with:")
 
29
  print()
30
  with st.spinner('Fetching AI response...'):
31
  # 构造请求
32
+ # FastAPI 服务器的 URL
33
+ fastapi_server_url = "https://binqiangliu-wechatarticleloaderfastapi.hf.space/get_ai_response"
34
+ headers = {
35
+ "Content-Type": "application/json",
36
+ "Authorization": f"Bearer {HUGGINGFACEHUB_API_TOKEN}"
37
+ #保险起见,建议始终采用f''的形式以及配合使用{}
38
+ }
39
  data = {"url": url, "question": question}
40
  # 发送请求到 FastAPI 服务器
41
  current_datetime_0 = datetime.datetime.now()
42
  print(f'API调用请求发送开始 @ {current_datetime_0}')
43
  print()
44
  start_1 = timeit.default_timer() # Start timer
45
+ #response = requests.post(fastapi_server_url, json=data)
46
+ response = requests.post(fastapi_server_url, headers=headers, json=data)
47
  end_1 = timeit.default_timer() # Start timer
48
  print(f'API调用请求发送结束,共耗时: @ {end_1 - start_1}')
49
  print()