Spaces:
Sleeping
Sleeping
matthewfarant
commited on
Commit
•
c80a1a0
1
Parent(s):
83f1ea4
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Initialize LLM and Tools
|
2 |
+
local_llm = 'llama3.1'
|
3 |
+
llama3 = ChatOllama(model=local_llm, temperature=1)
|
4 |
+
llama3_json = ChatOllama(model=local_llm, format='json', temperature=0)
|
5 |
+
google_search = GoogleSearchAPIWrapper()
|
6 |
+
os.environ["GOOGLE_CSE_ID"] = "72150c1d158b54108"
|
7 |
+
os.environ["GOOGLE_API_KEY"] = "AIzaSyAaNoF_a27HM3C87ObELRLTeVCrke_3OJA"
|
8 |
+
firecrawl_app = FirecrawlApp(api_key='fc-beeb8af53ea6460fbe9f5c2997c7b39b')
|
9 |
+
|
10 |
+
# Query Transformation
|
11 |
+
query_prompt = PromptTemplate(
|
12 |
+
template="""
|
13 |
+
|
14 |
+
<|begin_of_text|>
|
15 |
+
|
16 |
+
<|start_header_id|>system<|end_header_id|>
|
17 |
+
|
18 |
+
You are an expert at crafting web search queries for fact checking.
|
19 |
+
More often than not, a user will provide an information that they wish to fact check, however it might not be in the best format.
|
20 |
+
Reword their query to be the most effective web search string possible.
|
21 |
+
Return the JSON with a single key 'query' with no premable or explanation.
|
22 |
+
|
23 |
+
Information to transform: {question}
|
24 |
+
|
25 |
+
<|eot_id|>
|
26 |
+
|
27 |
+
<|start_header_id|>assistant<|end_header_id|>
|
28 |
+
|
29 |
+
""",
|
30 |
+
input_variables=["question"],
|
31 |
+
)
|
32 |
+
|
33 |
+
# Chain
|
34 |
+
query_chain = query_prompt | llama3_json | JsonOutputParser()
|
35 |
+
|
36 |
+
# Google Search and Firecrawl Setup
|
37 |
+
def search_and_scrape(keyword):
|
38 |
+
search_results = google_search.results(keyword, 3)
|
39 |
+
scraped_data = []
|
40 |
+
for result in search_results:
|
41 |
+
url = result['link']
|
42 |
+
scrape_response = firecrawl_app.scrape_url(url=url, params={'formats': ['markdown']})
|
43 |
+
scraped_data.append(scrape_response)
|
44 |
+
return scraped_data
|
45 |
+
|
46 |
+
# Summarizer
|
47 |
+
summarize_prompt = PromptTemplate(
|
48 |
+
template="""
|
49 |
+
|
50 |
+
<|begin_of_text|>
|
51 |
+
|
52 |
+
<|start_header_id|>system<|end_header_id|>
|
53 |
+
|
54 |
+
You are an expert at summarizing web crawling results. The user will give you multiple web search result with different topics. Your task is to summarize all the important information
|
55 |
+
from the article in a readable paragraph. It is okay if one paragraph contains multiple topics.
|
56 |
+
|
57 |
+
Information to transform: {question}
|
58 |
+
|
59 |
+
<|eot_id|>
|
60 |
+
|
61 |
+
<|start_header_id|>assistant<|end_header_id|>
|
62 |
+
|
63 |
+
""",
|
64 |
+
input_variables=["question"],
|
65 |
+
)
|
66 |
+
|
67 |
+
# Chain
|
68 |
+
summarize_chain = summarize_prompt | llama3 | StrOutputParser()
|
69 |
+
|
70 |
+
# Generation prompt
|
71 |
+
generate_prompt = PromptTemplate(
|
72 |
+
template="""
|
73 |
+
|
74 |
+
<|begin_of_text|>
|
75 |
+
|
76 |
+
<|start_header_id|>system<|end_header_id|>
|
77 |
+
|
78 |
+
You are a fact-checker AI assistant that receives an information from the user, synthesizes web search results for that information, and verify whether the user's information is a fact or possibly a hoax.
|
79 |
+
Strictly use the following pieces of web search context to answer the question. If you don't know the answer, just give "Possibly Hoax" verdict. Only make direct references to material if provided in the context.
|
80 |
+
Return a JSON output with these keys, with no premable:
|
81 |
+
1. Verdict: choose between "Fact" and "Possibly Hoax"
|
82 |
+
2. Explanation: a short explanation on why the verdict was chosen
|
83 |
+
If the context does not relate with the information provided by user, you can give "Possibly Hoax" result and tell the user that based on web search, it seems that the provided information is a false information.
|
84 |
+
|
85 |
+
<|eot_id|>
|
86 |
+
|
87 |
+
<|start_header_id|>user<|end_header_id|>
|
88 |
+
|
89 |
+
User Information: {question}
|
90 |
+
Web Search Context: {context}
|
91 |
+
JSON Verdict and Explanation:
|
92 |
+
|
93 |
+
<|eot_id|>
|
94 |
+
|
95 |
+
<|start_header_id|>assistant<|end_header_id|>
|
96 |
+
""",
|
97 |
+
input_variables=["question", "context"],
|
98 |
+
)
|
99 |
+
|
100 |
+
# Chain
|
101 |
+
generate_chain = generate_prompt | llama3_json | JsonOutputParser()
|
102 |
+
|
103 |
+
# Full Flow Function
|
104 |
+
def fact_check_flow(user_question):
|
105 |
+
# Step 2: Transform question into search query keyword
|
106 |
+
keyword = query_chain.invoke({"question": user_question})["query"]
|
107 |
+
|
108 |
+
# Step 3 & 4: Google search and scrape results
|
109 |
+
context_data = search_and_scrape(keyword)
|
110 |
+
|
111 |
+
final_markdown = []
|
112 |
+
for results in context_data:
|
113 |
+
final_markdown.append(results['markdown'])
|
114 |
+
|
115 |
+
final_markdown = ' '.join(final_markdown)
|
116 |
+
|
117 |
+
context = summarize_chain.invoke({"question": final_markdown})
|
118 |
+
|
119 |
+
# Step 5: Use scraped data as context and run generate chain
|
120 |
+
|
121 |
+
final_response = generate_chain.invoke({"question": user_question, "context": context})
|
122 |
+
|
123 |
+
return final_response
|
124 |
+
|
125 |
+
# Example Use
|
126 |
+
# user_question = "biden is not joining election in 2024"
|
127 |
+
# result = fact_check_flow(user_question)
|
128 |
+
# print(result)
|
129 |
+
|
130 |
+
demo = gr.Interface(fn=fact_check_flow, inputs="textbox", outputs="textbox")
|
131 |
+
|
132 |
+
if __name__ == "__main__":
|
133 |
+
demo.launch()
|