Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,11 +13,11 @@ import time
|
|
| 13 |
os.environ["OPENAI_API_KEY"] = "sb-f6c206ba7ea4df761e227eba65e65bedad1e24e73b702967"
|
| 14 |
os.environ['OPENAI_BASE_URL'] = "https://api.openai-sb.com/v1/"
|
| 15 |
os.environ["SERPAPI_API_KEY"] = "dcc98b22d5f7d413979a175ff7d75b721c5992a3ee1e2363020b2bbdf4f82404"
|
| 16 |
-
os.environ['TAVILY_API_KEY'] = "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"
|
|
|
|
| 17 |
|
| 18 |
llm = ChatOpenAI(model="gpt-4o", temperature=0.6)
|
| 19 |
|
| 20 |
-
|
| 21 |
def search(query) -> str:
|
| 22 |
params = {
|
| 23 |
"engine": "baidu_news",
|
|
@@ -131,35 +131,45 @@ Thought: {agent_scratchpad}
|
|
| 131 |
origin_prompt = ChatPromptTemplate.from_template(template)
|
| 132 |
chain = origin_prompt | llm | StrOutputParser()
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
-
def react(input
|
| 136 |
ori_result = chain.invoke(input)
|
| 137 |
tools = [TavilySearchResults(max_result=1),search_tool]
|
| 138 |
-
if
|
| 139 |
prompt = ChatPromptTemplate.from_template(en_prompt)
|
| 140 |
-
elif
|
| 141 |
prompt = ChatPromptTemplate.from_template(ch_prompt)
|
| 142 |
agent = create_react_agent(llm, tools, prompt)
|
| 143 |
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True, handle_parsing_errors=True)
|
| 144 |
result = agent_executor.invoke({"input": input})
|
| 145 |
return result['output'], ori_result
|
|
|
|
|
|
|
| 146 |
title = "# 虚假信息检测"
|
| 147 |
|
| 148 |
with gr.Blocks(css=css1) as demo:
|
| 149 |
-
# gr.Markdown(
|
| 150 |
-
# "<h1 style='font-size: 36px; text-align: center; color: #333333; margin-bottom: 20px;'>虚假信息检测</h1>")
|
| 151 |
gr.Markdown(title, elem_id="title")
|
| 152 |
with gr.Row():
|
| 153 |
with gr.Column(scale=2):
|
| 154 |
chatbot = gr.Chatbot(elem_classes="gradio-output")
|
| 155 |
-
|
| 156 |
-
|
|
|
|
| 157 |
with gr.Column(scale=1):
|
| 158 |
ans_box = gr.Textbox(label="gpt-4o", lines=5, elem_classes="gradio-output")
|
| 159 |
-
react_box = gr.Textbox(label="React", lines=5, elem_classes="gradio-output")
|
| 160 |
clear = gr.Button("清空页面", elem_classes="gradio-button")
|
| 161 |
submit_btn = gr.Button("提交", elem_classes="gradio-button")
|
| 162 |
-
|
| 163 |
|
| 164 |
def user(user_input, history):
|
| 165 |
if history is None:
|
|
@@ -177,9 +187,7 @@ with gr.Blocks(css=css1) as demo:
|
|
| 177 |
time.sleep(0.01)
|
| 178 |
yield history
|
| 179 |
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
submit_btn.click(react, [input_box, language_box], [react_box, ans_box]
|
| 183 |
).then(
|
| 184 |
user, [input_box, chatbot], [input_box, chatbot]
|
| 185 |
).then(
|
|
@@ -189,4 +197,3 @@ with gr.Blocks(css=css1) as demo:
|
|
| 189 |
|
| 190 |
if __name__ == "__main__":
|
| 191 |
demo.launch()
|
| 192 |
-
|
|
|
|
| 13 |
os.environ["OPENAI_API_KEY"] = "sb-f6c206ba7ea4df761e227eba65e65bedad1e24e73b702967"
|
| 14 |
os.environ['OPENAI_BASE_URL'] = "https://api.openai-sb.com/v1/"
|
| 15 |
os.environ["SERPAPI_API_KEY"] = "dcc98b22d5f7d413979a175ff7d75b721c5992a3ee1e2363020b2bbdf4f82404"
|
| 16 |
+
# os.environ['TAVILY_API_KEY'] = "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" #you
|
| 17 |
+
os.environ['TAVILY_API_KEY'] = "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" #zeng
|
| 18 |
|
| 19 |
llm = ChatOpenAI(model="gpt-4o", temperature=0.6)
|
| 20 |
|
|
|
|
| 21 |
def search(query) -> str:
|
| 22 |
params = {
|
| 23 |
"engine": "baidu_news",
|
|
|
|
| 131 |
origin_prompt = ChatPromptTemplate.from_template(template)
|
| 132 |
chain = origin_prompt | llm | StrOutputParser()
|
| 133 |
|
| 134 |
+
selected_language = "ch"
|
| 135 |
+
|
| 136 |
+
def SelectLanguage(option):
|
| 137 |
+
global selected_language
|
| 138 |
+
if option == "英文":
|
| 139 |
+
selected_language = "en"
|
| 140 |
+
else:
|
| 141 |
+
selected_language = "ch"
|
| 142 |
+
language_option = ["中文","英文"]
|
| 143 |
|
| 144 |
+
def react(input):
|
| 145 |
ori_result = chain.invoke(input)
|
| 146 |
tools = [TavilySearchResults(max_result=1),search_tool]
|
| 147 |
+
if selected_language == "en":
|
| 148 |
prompt = ChatPromptTemplate.from_template(en_prompt)
|
| 149 |
+
elif selected_language == "ch":
|
| 150 |
prompt = ChatPromptTemplate.from_template(ch_prompt)
|
| 151 |
agent = create_react_agent(llm, tools, prompt)
|
| 152 |
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True, handle_parsing_errors=True)
|
| 153 |
result = agent_executor.invoke({"input": input})
|
| 154 |
return result['output'], ori_result
|
| 155 |
+
|
| 156 |
+
|
| 157 |
title = "# 虚假信息检测"
|
| 158 |
|
| 159 |
with gr.Blocks(css=css1) as demo:
|
|
|
|
|
|
|
| 160 |
gr.Markdown(title, elem_id="title")
|
| 161 |
with gr.Row():
|
| 162 |
with gr.Column(scale=2):
|
| 163 |
chatbot = gr.Chatbot(elem_classes="gradio-output")
|
| 164 |
+
language_select = gr.Dropdown(choices= language_option, elem_classes="gradio-input", label= "请选择要使用的语言")
|
| 165 |
+
input_box = gr.Textbox(label="输入", elem_classes="gradio-input", placeholder="请输入要判断的新闻", lines=3)
|
| 166 |
+
|
| 167 |
with gr.Column(scale=1):
|
| 168 |
ans_box = gr.Textbox(label="gpt-4o", lines=5, elem_classes="gradio-output")
|
| 169 |
+
react_box = gr.Textbox(label="React", lines=5, elem_classes="gradio-output", visible=False)
|
| 170 |
clear = gr.Button("清空页面", elem_classes="gradio-button")
|
| 171 |
submit_btn = gr.Button("提交", elem_classes="gradio-button")
|
| 172 |
+
language_select.change(SelectLanguage, language_select)
|
| 173 |
|
| 174 |
def user(user_input, history):
|
| 175 |
if history is None:
|
|
|
|
| 187 |
time.sleep(0.01)
|
| 188 |
yield history
|
| 189 |
|
| 190 |
+
submit_btn.click(react, [input_box], [react_box, ans_box]
|
|
|
|
|
|
|
| 191 |
).then(
|
| 192 |
user, [input_box, chatbot], [input_box, chatbot]
|
| 193 |
).then(
|
|
|
|
| 197 |
|
| 198 |
if __name__ == "__main__":
|
| 199 |
demo.launch()
|
|
|