Update app.py
Browse files
app.py
CHANGED
@@ -15,6 +15,19 @@ load_dotenv()
|
|
15 |
hf_token = os.environ.get('HUGGINGFACEHUB_API_TOKEN')
|
16 |
repo_id=os.environ.get('repo_id')
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
#port = os.getenv('port')
|
19 |
|
20 |
#OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
|
@@ -45,27 +58,25 @@ if url !="" and not url.strip().isspace() and not url == "" and not url.strip()
|
|
45 |
try:
|
46 |
#loader = WebBaseLoader("https://www.usinoip.com/")
|
47 |
with st.spinner("AI Thinking...Please wait a while to Cheers!"):
|
48 |
-
print(
|
49 |
loader = WebBaseLoader(url)
|
50 |
-
print("2")
|
51 |
docs = loader.load()
|
52 |
-
print(docs)
|
53 |
split_docs = text_splitter_rcs.split_documents(docs)
|
54 |
-
print("3")
|
55 |
-
print("split_docs")
|
56 |
#result=chain.run(docs) #这个result的格式比较特殊,可以直接print,但不可以和其他字符串联合print输出 - this step errors!
|
57 |
result=chain.run(split_docs)
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
62 |
#print("AI Summarization: "+result) #这个会出错,原因见上方
|
63 |
print("AI Summarization:")
|
64 |
#print(result)
|
65 |
-
print(
|
66 |
-
|
67 |
st.write("AI Summarization:")
|
68 |
#st.write(result)
|
69 |
-
st.write(
|
70 |
except Exception as e:
|
71 |
st.write("Wrong URL or URL not parsable.")
|
|
|
15 |
hf_token = os.environ.get('HUGGINGFACEHUB_API_TOKEN')
|
16 |
repo_id=os.environ.get('repo_id')
|
17 |
|
18 |
+
print(f"定义处理多余的Context文本的函数")
|
19 |
+
def remove_context(text):
|
20 |
+
# 检查 'Context:' 是否存在
|
21 |
+
if 'Context:' in text:
|
22 |
+
# 找到第一个 '\n\n' 的位置
|
23 |
+
end_of_context = text.find('\n\n')
|
24 |
+
# 删除 'Context:' 到第一个 '\n\n' 之间的部分
|
25 |
+
return text[end_of_context + 2:] # '+2' 是为了跳过两个换行符
|
26 |
+
else:
|
27 |
+
# 如果 'Context:' 不存在,返回原始文本
|
28 |
+
return text
|
29 |
+
print(f"处理多余的Context文本函数定义结束")
|
30 |
+
|
31 |
#port = os.getenv('port')
|
32 |
|
33 |
#OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
|
|
|
58 |
try:
|
59 |
#loader = WebBaseLoader("https://www.usinoip.com/")
|
60 |
with st.spinner("AI Thinking...Please wait a while to Cheers!"):
|
61 |
+
print(url)
|
62 |
loader = WebBaseLoader(url)
|
|
|
63 |
docs = loader.load()
|
|
|
64 |
split_docs = text_splitter_rcs.split_documents(docs)
|
|
|
|
|
65 |
#result=chain.run(docs) #这个result的格式比较特殊,可以直接print,但不可以和其他字符串联合print输出 - this step errors!
|
66 |
result=chain.run(split_docs)
|
67 |
+
initial_ai_response=str(result) #找到之前总是POST Error的原因:chain.run(docs)的结果,格式不是str,导致程序错误
|
68 |
+
cleaned_initial_ai_response = remove_context(initial_ai_response)
|
69 |
+
print(cleaned_initial_ai_response)
|
70 |
+
print()
|
71 |
+
final_ai_response = cleaned_initial_ai_response.split('<|end|>')[0].strip().replace('\n\n', '\n').replace('<|end|>', '').replace('<|user|>', '').replace('<|system|>', '').replace('<|assistant|>', '')
|
72 |
+
new_final_ai_response = final_ai_response.split('Unhelpful Answer:')[0].strip()
|
73 |
+
final_result = new_final_ai_response.split('Note:')[0].strip()
|
74 |
#print("AI Summarization: "+result) #这个会出错,原因见上方
|
75 |
print("AI Summarization:")
|
76 |
#print(result)
|
77 |
+
print(final_result)
|
|
|
78 |
st.write("AI Summarization:")
|
79 |
#st.write(result)
|
80 |
+
st.write(final_result)
|
81 |
except Exception as e:
|
82 |
st.write("Wrong URL or URL not parsable.")
|