File size: 4,001 Bytes
f236a9e
 
0c4b8aa
 
87ca47e
1d1683c
 
f236a9e
0136600
 
 
 
 
 
f236a9e
f8b49ee
 
 
 
1d1683c
f236a9e
 
 
99f5f75
f8b49ee
f236a9e
99f5f75
f8b49ee
f236a9e
 
 
6d1d4b7
 
 
cbcc1be
6d1d4b7
cbcc1be
f236a9e
f8b49ee
 
f236a9e
 
1d1683c
6a042d1
1d1683c
 
 
 
 
f236a9e
 
0c4b8aa
f8b49ee
 
0c4b8aa
1d1683c
 
0c4b8aa
 
f8b49ee
f236a9e
 
 
b34f902
c06de5c
 
f8b49ee
0c4b8aa
b34f902
5e092ec
0c4b8aa
 
f8b49ee
99f5f75
 
 
f236a9e
78043f5
f236a9e
 
 
 
0c4b8aa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import streamlit as st
import requests
import timeit
import datetime
import os
from dotenv import load_dotenv
load_dotenv()

st.set_page_config(page_title="WeChat Article AI Assistant - Open Source Version", layout="wide")
st.subheader("Welcome to Open WeChat Article AI Assistant: Life Enhancing with AI!")
st.write("Important notice: This Open WeChat Article AI Assistant is offered for information and study purpose only and by no means for any other use. Any user should never interact with the AI Assistant in any way that is against any related promulgated regulations. The user is the only entity responsible for interactions taken between the user and the AI Chat Assistant.")
css_file = "main.css"
with open(css_file) as f:
    st.markdown("<style>{}</style>".format(f.read()), unsafe_allow_html=True)  

current_datetime_0= datetime.datetime.now()
print(f"Anything happens, this ST app will execute from top down. @ {current_datetime_0}")
print()

HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")

# 用户输入
url = st.text_input("Enter the URL to chat with:")
print(f"URL Entered: "+url)
print()
question = st.text_input("Enter your question:")
print(f"Question Entered: "+question)
print()

# 当用户点击按钮时
if st.button('Get AI Response'):
    # 检查 URL 是否以 "https://" 或 'http://' 开始
    #if not url.startswith("https://mp.weixin.qq.com"):
    if not url.startswith(("https://", "http://")):
        # 如果不是,则显示错误信息并终止程序
        st.write("URL is incorrect. Please enter a URL that starts with 'https://' or 'http://'.")
        st.stop()
    if url and question:
        print(f"URL & Question Entered & Button clicked.")
        print()
        with st.spinner('Fetching AI response...'):
            # 构造请求
            # FastAPI 服务器的 URL
            fastapi_server_url = "https://binqiangliu-webpagereaderfastapi.hf.space/get_ai_response"
            headers = {
                "Content-Type": "application/json",
                "Authorization": f"Bearer {HUGGINGFACEHUB_API_TOKEN}"
                #保险起见,建议始终采用f''的形式以及配合使用{}
            }
            data = {"url": url, "question": question}
            # 发送请求到 FastAPI 服务器
            current_datetime_0 = datetime.datetime.now()
            print(f'API调用请求发送开始 @ {current_datetime_0}')   
            print()
            start_1 = timeit.default_timer() # Start timer               
            #response = requests.post(fastapi_server_url, json=data)   
            response = requests.post(fastapi_server_url, headers=headers, json=data)            
            end_1 = timeit.default_timer() # Start timer   
            print(f'API调用请求发送结束,共耗时: @ {end_1 - start_1}')  
            print()
            
            if response.status_code == 200:
                # 显示 AI 的回答
                #ai_response = response.json().get("AI Response", "No response received.")
                current_datetime_1 = datetime.datetime.now()
                print(f'获取API调用结果开始 @ {current_datetime_1}')
                print()
                start_2 = timeit.default_timer() # Start timer   
                ai_response = response.json()
                ai_response_output=ai_response['AIResponse']
                end_2 = timeit.default_timer() # Start timer   
                print(f'获取API调用结果完毕,共耗时: @ {end_2 - start_2}') 
                print()
                print("AI Response:", ai_response)
                print("AI Response:", ai_response_output)    
                print()
                st.write("AI Response:", ai_response)
                st.write("AI Response:", ai_response_output)
            else:
                # 显示错误信息
                st.error("Error in fetching response from AI server.")
    else:
        st.warning("Please enter both URL and a question.")