matthewfarant commited on
Commit
c74009c
1 Parent(s): d565b10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -93
app.py CHANGED
@@ -20,9 +20,45 @@ os.environ["HUGGINGFACEHUB_API_TOKEN"] = os.getenv('HF_KEY')
20
  os.environ["GOOGLE_CSE_ID"] = os.getenv('GOOGLE_CSE_ID')
21
  os.environ["GOOGLE_API_KEY"] = os.getenv('GOOGLE_API_KEY')
22
 
 
 
 
 
 
 
 
 
 
 
23
  google_search = GoogleSearchAPIWrapper()
24
  firecrawl_app = FirecrawlApp(api_key=os.getenv('FIRECRAWL_KEY'))
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  # Google Search and Firecrawl Setup
27
  def search_and_scrape(keyword):
28
  search_results = google_search.results(keyword, 3)
@@ -33,103 +69,66 @@ def search_and_scrape(keyword):
33
  scraped_data.append(scrape_response)
34
  return scraped_data
35
 
36
- # Full Flow Function
37
- def fact_check_flow(user_question):
38
- # Step 1 : Initializing
39
- llm = HuggingFaceEndpoint(
40
- repo_id="meta-llama/Meta-Llama-3.1-8B-Instruct",
41
- task="text-generation",
42
- max_new_tokens=4000,
43
- do_sample=False,
44
- repetition_penalty=1.03,
45
- )
46
- llama3 = ChatHuggingFace(llm=llm, temperature = 1)
47
- llama3_json = ChatHuggingFace(llm=llm, format = 'json', temperature = 0)
48
-
49
- # Query Transformation
50
- query_prompt = PromptTemplate(
51
- template="""
52
-
53
- <|begin_of_text|>
54
-
55
- <|start_header_id|>system<|end_header_id|>
56
-
57
- You are an expert at crafting web search queries for fact checking.
58
- 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.
59
- Reword their query to be the most effective web search string possible.
60
- Return the JSON with a single key 'query' with no premable or explanation.
61
-
62
- Information to transform: {question}
63
-
64
- <|eot_id|>
65
-
66
- <|start_header_id|>assistant<|end_header_id|>
67
-
68
- """,
69
- input_variables=["question"],
70
- )
71
 
72
- # Chain
73
- query_chain = query_prompt | llama3_json | JsonOutputParser()
74
-
75
- # Summarizer
76
- summarize_prompt = PromptTemplate(
77
- template="""
78
-
79
- <|begin_of_text|>
80
-
81
- <|start_header_id|>system<|end_header_id|>
82
-
83
- 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
84
- from the article in a readable paragraph. It is okay if one paragraph contains multiple topics.
85
-
86
- Information to transform: {question}
87
-
88
- <|eot_id|>
89
-
90
- <|start_header_id|>assistant<|end_header_id|>
91
-
92
- """,
93
- input_variables=["question"],
94
- )
95
 
96
- # Chain
97
- summarize_chain = summarize_prompt | llama3 | StrOutputParser()
98
-
99
- # Generation prompt
100
- generate_prompt = PromptTemplate(
101
- template="""
102
-
103
- <|begin_of_text|>
104
-
105
- <|start_header_id|>system<|end_header_id|>
106
-
107
- 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.
108
- 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.
109
- Return a JSON output with these keys, with no premable:
110
- 1. user_information: the user's input
111
- 2. system_verdict: is the user question above a fact? choose only between "Fact" or "Possibly Hoax"
112
- 3. explanation: a short explanation on why the verdict was chosen
113
- 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.
114
-
115
- <|eot_id|>
116
-
117
- <|start_header_id|>user<|end_header_id|>
118
-
119
- User Information: {question}
120
- Web Search Context: {context}
121
- JSON output:
122
-
123
- <|eot_id|>
124
-
125
- <|start_header_id|>assistant<|end_header_id|>
126
- """,
127
- input_variables=["question", "context"],
128
- )
129
 
130
- # Chain
131
- generate_chain = generate_prompt | llama3_json | JsonOutputParser()
132
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  # Step 2: Transform question into search query keyword
134
  keyword = query_chain.invoke({"question": user_question})["query"]
135
 
@@ -150,6 +149,11 @@ def fact_check_flow(user_question):
150
 
151
  return final_response
152
 
 
 
 
 
 
153
  demo = gr.Interface(fn=fact_check_flow, inputs="textbox", outputs="textbox")
154
 
155
  if __name__ == "__main__":
 
20
  os.environ["GOOGLE_CSE_ID"] = os.getenv('GOOGLE_CSE_ID')
21
  os.environ["GOOGLE_API_KEY"] = os.getenv('GOOGLE_API_KEY')
22
 
23
+ llm = HuggingFaceEndpoint(
24
+ repo_id="meta-llama/Meta-Llama-3.1-8B-Instruct",
25
+ task="text-generation",
26
+ max_new_tokens=4000,
27
+ do_sample=False,
28
+ repetition_penalty=1.03,
29
+ )
30
+ llama3 = ChatHuggingFace(llm=llm, temperature = 1)
31
+ llama3_json = ChatHuggingFace(llm=llm, format = 'json', temperature = 0)
32
+
33
  google_search = GoogleSearchAPIWrapper()
34
  firecrawl_app = FirecrawlApp(api_key=os.getenv('FIRECRAWL_KEY'))
35
 
36
+ # Query Transformation
37
+ query_prompt = PromptTemplate(
38
+ template="""
39
+
40
+ <|begin_of_text|>
41
+
42
+ <|start_header_id|>system<|end_header_id|>
43
+
44
+ You are an expert at crafting web search queries for fact checking.
45
+ 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.
46
+ Reword their query to be the most effective web search string possible.
47
+ Return the JSON with a single key 'query' with no premable or explanation.
48
+
49
+ Information to transform: {question}
50
+
51
+ <|eot_id|>
52
+
53
+ <|start_header_id|>assistant<|end_header_id|>
54
+
55
+ """,
56
+ input_variables=["question"],
57
+ )
58
+
59
+ # Chain
60
+ query_chain = query_prompt | llama3_json | JsonOutputParser()
61
+
62
  # Google Search and Firecrawl Setup
63
  def search_and_scrape(keyword):
64
  search_results = google_search.results(keyword, 3)
 
69
  scraped_data.append(scrape_response)
70
  return scraped_data
71
 
72
+ # Summarizer
73
+ summarize_prompt = PromptTemplate(
74
+ template="""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
+ <|begin_of_text|>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
+ <|start_header_id|>system<|end_header_id|>
79
+
80
+ 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
81
+ from the article in a readable paragraph. It is okay if one paragraph contains multiple topics.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
+ Information to transform: {question}
 
84
 
85
+ <|eot_id|>
86
+
87
+ <|start_header_id|>assistant<|end_header_id|>
88
+
89
+ """,
90
+ input_variables=["question"],
91
+ )
92
+
93
+ # Chain
94
+ summarize_chain = summarize_prompt | llama3 | StrOutputParser()
95
+
96
+ # Generation prompt
97
+ generate_prompt = PromptTemplate(
98
+ template="""
99
+
100
+ <|begin_of_text|>
101
+
102
+ <|start_header_id|>system<|end_header_id|>
103
+
104
+ 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.
105
+ 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.
106
+ Return a JSON output with these keys, with no premable:
107
+ 1. user_information: the user's input
108
+ 2. system_verdict: is the user question above a fact? choose only between "Fact" or "Possibly Hoax"
109
+ 3. explanation: a short explanation on why the verdict was chosen
110
+ 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.
111
+
112
+ <|eot_id|>
113
+
114
+ <|start_header_id|>user<|end_header_id|>
115
+
116
+ User Information: {question}
117
+ Web Search Context: {context}
118
+ JSON Verdict and Explanation:
119
+
120
+ <|eot_id|>
121
+
122
+ <|start_header_id|>assistant<|end_header_id|>
123
+ """,
124
+ input_variables=["question", "context"],
125
+ )
126
+
127
+ # Chain
128
+ generate_chain = generate_prompt | llama3_json | JsonOutputParser()
129
+
130
+ # Full Flow Function
131
+ def fact_check_flow(user_question):
132
  # Step 2: Transform question into search query keyword
133
  keyword = query_chain.invoke({"question": user_question})["query"]
134
 
 
149
 
150
  return final_response
151
 
152
+ # Example Use
153
+ # user_question = "biden is not joining election in 2024"
154
+ # result = fact_check_flow(user_question)
155
+ # print(result)
156
+
157
  demo = gr.Interface(fn=fact_check_flow, inputs="textbox", outputs="textbox")
158
 
159
  if __name__ == "__main__":