iShare commited on
Commit
2467b5a
·
1 Parent(s): 9860b3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -3
app.py CHANGED
@@ -31,6 +31,19 @@ llm = HuggingFaceHub(repo_id=repo_id, # for StarChat
31
 
32
  chain = load_summarize_chain(llm, chain_type="refine")
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  #text_splitter_rcs = RecursiveCharacterTextSplitter(
35
  # #separator = "\n", #TypeError: TextSplitter.__init__() got an unexpected keyword argument 'separator'
36
  # chunk_size = 500,
@@ -46,18 +59,26 @@ if url !="" and not url.strip().isspace() and not url == "" and not url.strip()
46
  try:
47
  #loader = WebBaseLoader("https://www.usinoip.com/")
48
  with st.spinner("AI Thinking...Please wait a while to Cheers!"):
49
- print("Website to Chat: "+url)
 
50
  loader = WebBaseLoader(url)
51
  docs = loader.load()
52
  print("Webpage contents loaded")
 
53
  #split_docs = text_splitter_rcs.split_documents(docs)
54
  #print(split_docs)
55
  result=chain.run(docs) #这个result的格式比较特殊,可以直接print,但不可以和其他字符串联合print输出 - this step errors!
56
  #result=chain.run(split_docs) #找到之前总是POST Error的原因:chain.run(docs)的结果,格式不是str,导致程序错误
57
- print("Chain run finished")
 
 
58
  result=str(result)
 
 
 
59
  cleaned_initial_ai_response = remove_context(result)
60
- print("Ai Resposne result cleaned initially: "+cleaned_initial_ai_response)
 
61
  final_ai_response = cleaned_initial_ai_response.split('<|end|>')[0].strip().replace('\n\n', '\n').replace('<|end|>', '').replace('<|user|>', '').replace('<|system|>', '').replace('<|assistant|>', '')
62
  new_final_ai_response = final_ai_response.split('Unhelpful Answer:')[0].strip()
63
  final_result = new_final_ai_response.split('Note:')[0].strip()
 
31
 
32
  chain = load_summarize_chain(llm, chain_type="refine")
33
 
34
+ print(f"定义处理多余的Context文本的函数")
35
+ def remove_context(text):
36
+ # 检查 'Context:' 是否存在
37
+ if 'Context:' in text:
38
+ # 找到第一个 '\n\n' 的位置
39
+ end_of_context = text.find('\n\n')
40
+ # 删除 'Context:' 到第一个 '\n\n' 之间的部分
41
+ return text[end_of_context + 2:] # '+2' 是为了跳过两个换行符
42
+ else:
43
+ # 如果 'Context:' 不存在,返回原始文本
44
+ return text
45
+ print(f"处理多余的Context文本函数定义结束")
46
+
47
  #text_splitter_rcs = RecursiveCharacterTextSplitter(
48
  # #separator = "\n", #TypeError: TextSplitter.__init__() got an unexpected keyword argument 'separator'
49
  # chunk_size = 500,
 
59
  try:
60
  #loader = WebBaseLoader("https://www.usinoip.com/")
61
  with st.spinner("AI Thinking...Please wait a while to Cheers!"):
62
+ print("Website to Chat: "+url)
63
+
64
  loader = WebBaseLoader(url)
65
  docs = loader.load()
66
  print("Webpage contents loaded")
67
+
68
  #split_docs = text_splitter_rcs.split_documents(docs)
69
  #print(split_docs)
70
  result=chain.run(docs) #这个result的格式比较特殊,可以直接print,但不可以和其他字符串联合print输出 - this step errors!
71
  #result=chain.run(split_docs) #找到之前总是POST Error的原因:chain.run(docs)的结果,格式不是str,导致程序错误
72
+ print("Chain run results:")
73
+ print(result)
74
+
75
  result=str(result)
76
+ print("Chain run results in str format:")
77
+ print(result)
78
+
79
  cleaned_initial_ai_response = remove_context(result)
80
+ print("Ai Resposne result cleaned initially: "+cleaned_initial_ai_response)
81
+
82
  final_ai_response = cleaned_initial_ai_response.split('<|end|>')[0].strip().replace('\n\n', '\n').replace('<|end|>', '').replace('<|user|>', '').replace('<|system|>', '').replace('<|assistant|>', '')
83
  new_final_ai_response = final_ai_response.split('Unhelpful Answer:')[0].strip()
84
  final_result = new_final_ai_response.split('Note:')[0].strip()