binqiangliu
commited on
Commit
•
ec3656a
1
Parent(s):
328441c
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import streamlit as st
|
3 |
+
import os
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
load_dotenv()
|
6 |
+
import json # 导入json模块
|
7 |
+
|
8 |
+
st.set_page_config(page_title="USinoIP Website AI Chat Assistant - Open Source Version", layout="wide")
|
9 |
+
st.subheader("Welcome to USinoIP Website AI Chat Assistant - Open Source Version.")
|
10 |
+
st.write("Important notice: This USinoIP Website AI Chat Assistant is offered ONLY for the purpose of assisting users to better interact with USinoIP webiste contents 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.")
|
11 |
+
|
12 |
+
css_file = "main.css"
|
13 |
+
with open(css_file) as f:
|
14 |
+
st.markdown("<style>{}</style>".format(f.read()), unsafe_allow_html=True)
|
15 |
+
|
16 |
+
HUGGINGFACEHUB_API_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN')
|
17 |
+
|
18 |
+
def call_chatbot_api(query):
|
19 |
+
#url = 'https://binqiangliu-fastapi-in-docker.hf.space/api/chat'
|
20 |
+
url = 'https://binqiangliu-officialusinositechatv1api.hf.space/api/chat'
|
21 |
+
headers = {
|
22 |
+
"Content-Type": "application/json",
|
23 |
+
"Authorization": f"Bearer {HUGGINGFACEHUB_API_TOKEN}"
|
24 |
+
}
|
25 |
+
json_data_for_api = {'user_question': query}
|
26 |
+
|
27 |
+
#response = requests.post(url, json=json_data_for_api)
|
28 |
+
#response = requests.post(url, headers=headers, json=json_data_for_api) #This format is working
|
29 |
+
#response = requests.post(url, headers=headers, data=json.dumps(data)) #NameError: name 'json' is not defined
|
30 |
+
response = requests.post(url, headers=headers, data=json.dumps(json_data_for_api)) #This format needs 'import json', or else NameError: name 'json' is not defined
|
31 |
+
|
32 |
+
result = response.json()
|
33 |
+
return result['response']
|
34 |
+
|
35 |
+
user_query = st.text_input("Enter your query here:")
|
36 |
+
if st.button('Get AI Response'):
|
37 |
+
with st.spinner("AI Thinking...Please wait a while to Cheers!"):
|
38 |
+
if user_query !="" and not user_query.strip().isspace() and not user_query == "" and not user_query.strip() == "" and not user_query.isspace():
|
39 |
+
response = call_chatbot_api(user_query)
|
40 |
+
st.write("USino AI Response:")
|
41 |
+
st.write(response)
|
42 |
+
print(response) # 打印Chatbot的响应
|