Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
@@ -28,6 +28,19 @@ llm = HuggingFaceHub(repo_id=repo_id, # for StarChat
|
|
28 |
#chain = load_summarize_chain(llm, chain_type="stuff")
|
29 |
chain = load_summarize_chain(llm, chain_type="refine")
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
app = FastAPI()
|
32 |
class ChatRequest(BaseModel):
|
33 |
target_url: str
|
@@ -56,11 +69,22 @@ async def chat(chat_request: ChatRequest):
|
|
56 |
print(target_url)
|
57 |
docs = loader.load()
|
58 |
result = chain.run(docs)
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
#result="Hi FastAPI" #用这个调试OK
|
61 |
-
print("AI Summarization: " +
|
62 |
#return {'response': result} #FastAPI方式下,这个返回形式,有问题? - NO!不是的!可以工作!
|
63 |
-
return JSONResponse({'response':
|
64 |
|
65 |
#对比FlaskAPI的:return jsonify({'response': result}),JSONResponse vs jsonify(都需要相应的from ... import ...)
|
66 |
except Exception as e:
|
|
|
28 |
#chain = load_summarize_chain(llm, chain_type="stuff")
|
29 |
chain = load_summarize_chain(llm, chain_type="refine")
|
30 |
|
31 |
+
print(f"定义处理多余的Context文本的函数")
|
32 |
+
def remove_context(text):
|
33 |
+
# 检查 'Context:' 是否存在
|
34 |
+
if 'Context:' in text:
|
35 |
+
# 找到第一个 '\n\n' 的位置
|
36 |
+
end_of_context = text.find('\n\n')
|
37 |
+
# 删除 'Context:' 到第一个 '\n\n' 之间的部分
|
38 |
+
return text[end_of_context + 2:] # '+2' 是为了跳过两个换行符
|
39 |
+
else:
|
40 |
+
# 如果 'Context:' 不存在,返回原始文本
|
41 |
+
return text
|
42 |
+
print(f"处理多余的Context文本函数定义结束")
|
43 |
+
|
44 |
app = FastAPI()
|
45 |
class ChatRequest(BaseModel):
|
46 |
target_url: str
|
|
|
69 |
print(target_url)
|
70 |
docs = loader.load()
|
71 |
result = chain.run(docs)
|
72 |
+
print(result)
|
73 |
+
print()
|
74 |
+
|
75 |
+
result=str(result) #找到之前总是POST Error的原因:chain.run(docs)的结果,格式不是str,导致{...}的json故障所致
|
76 |
+
cleaned_initial_ai_response = remove_context(result)
|
77 |
+
print(cleaned_initial_ai_response)
|
78 |
+
print()
|
79 |
+
|
80 |
+
final_ai_response = cleaned_initial_ai_response.split('<|end|>')[0].strip().replace('\n\n', '\n').replace('<|end|>', '').replace('<|user|>', '').replace('<|system|>', '').replace('<|assistant|>', '')
|
81 |
+
new_final_ai_response = final_ai_response.split('Unhelpful Answer:')[0].strip()
|
82 |
+
final_result = new_final_ai_response.split('Note:')[0].strip()
|
83 |
+
|
84 |
#result="Hi FastAPI" #用这个调试OK
|
85 |
+
print("AI Summarization: " + final_result)
|
86 |
#return {'response': result} #FastAPI方式下,这个返回形式,有问题? - NO!不是的!可以工作!
|
87 |
+
return JSONResponse({'response': final_result}) #这个形式也是OK的 - 只要result是字符形式即可?
|
88 |
|
89 |
#对比FlaskAPI的:return jsonify({'response': result}),JSONResponse vs jsonify(都需要相应的from ... import ...)
|
90 |
except Exception as e:
|