Spaces:
Sleeping
Sleeping
matthewfarant
commited on
Commit
•
14ff39f
1
Parent(s):
b1a47bd
Update app.py
Browse files
app.py
CHANGED
@@ -9,17 +9,18 @@ from firecrawl import FirecrawlApp
|
|
9 |
import gradio as gr
|
10 |
import os
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
# local_llm = 'llama3.1'
|
15 |
# llama3 = ChatOllama(model=local_llm, temperature=1)
|
16 |
# llama3_json = ChatOllama(model=local_llm, format='json', temperature=0)
|
17 |
|
18 |
-
#
|
19 |
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 |
llm = HuggingFaceEndpoint(
|
24 |
repo_id="meta-llama/Meta-Llama-3.1-8B-Instruct",
|
25 |
task="text-generation",
|
@@ -27,13 +28,15 @@ llm = HuggingFaceEndpoint(
|
|
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 |
-
#
|
37 |
query_prompt = PromptTemplate(
|
38 |
template="""
|
39 |
|
@@ -55,8 +58,7 @@ query_prompt = PromptTemplate(
|
|
55 |
""",
|
56 |
input_variables=["question"],
|
57 |
)
|
58 |
-
|
59 |
-
# Chain
|
60 |
query_chain = query_prompt | llama3_json | JsonOutputParser()
|
61 |
|
62 |
# Google Search and Firecrawl Setup
|
@@ -69,7 +71,7 @@ def search_and_scrape(keyword):
|
|
69 |
scraped_data.append(scrape_response)
|
70 |
return scraped_data
|
71 |
|
72 |
-
# Summarizer
|
73 |
summarize_prompt = PromptTemplate(
|
74 |
template="""
|
75 |
|
@@ -89,11 +91,10 @@ summarize_prompt = PromptTemplate(
|
|
89 |
""",
|
90 |
input_variables=["question"],
|
91 |
)
|
92 |
-
|
93 |
-
# Chain
|
94 |
summarize_chain = summarize_prompt | llama3 | StrOutputParser()
|
95 |
|
96 |
-
#
|
97 |
generate_prompt = PromptTemplate(
|
98 |
template="""
|
99 |
|
@@ -123,31 +124,31 @@ generate_prompt = PromptTemplate(
|
|
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 |
-
#
|
133 |
keyword = query_chain.invoke({"question": user_question})["query"]
|
134 |
|
135 |
-
#
|
136 |
context_data = search_and_scrape(keyword)
|
137 |
-
|
|
|
138 |
final_markdown = []
|
139 |
for results in context_data:
|
140 |
final_markdown.append(results['markdown'])
|
141 |
|
142 |
final_markdown = ' '.join(final_markdown)
|
143 |
|
|
|
144 |
context = summarize_chain.invoke({"question": final_markdown})
|
145 |
|
146 |
-
#
|
147 |
-
|
148 |
final_response = generate_chain.invoke({"question": user_question, "context": context})
|
149 |
|
150 |
-
# Process
|
151 |
verdict = final_response['system_verdict']
|
152 |
explanation = final_response['explanation']
|
153 |
|
@@ -160,11 +161,6 @@ def fact_check_flow(user_question):
|
|
160 |
|
161 |
return verdict_html + explanation_html
|
162 |
|
163 |
-
# Example Use
|
164 |
-
# user_question = "biden is not joining election in 2024"
|
165 |
-
# result = fact_check_flow(user_question)
|
166 |
-
# print(result)
|
167 |
-
|
168 |
demo = gr.Interface(
|
169 |
fn=fact_check_flow,
|
170 |
inputs=gr.Textbox(label="Input any information you want to fact-check!"),
|
|
|
9 |
import gradio as gr
|
10 |
import os
|
11 |
|
12 |
+
# Using Ollama (only in Local)
|
13 |
+
# -----------------------------
|
14 |
# local_llm = 'llama3.1'
|
15 |
# llama3 = ChatOllama(model=local_llm, temperature=1)
|
16 |
# llama3_json = ChatOllama(model=local_llm, format='json', temperature=0)
|
17 |
|
18 |
+
# Hugging Face Initialization
|
19 |
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 |
+
# Llama Endpoint
|
24 |
llm = HuggingFaceEndpoint(
|
25 |
repo_id="meta-llama/Meta-Llama-3.1-8B-Instruct",
|
26 |
task="text-generation",
|
|
|
28 |
do_sample=False,
|
29 |
repetition_penalty=1.03,
|
30 |
)
|
31 |
+
|
32 |
llama3 = ChatHuggingFace(llm=llm, temperature = 1)
|
33 |
llama3_json = ChatHuggingFace(llm=llm, format = 'json', temperature = 0)
|
34 |
|
35 |
+
# Search & Scrape Initialization
|
36 |
google_search = GoogleSearchAPIWrapper()
|
37 |
firecrawl_app = FirecrawlApp(api_key=os.getenv('FIRECRAWL_KEY'))
|
38 |
|
39 |
+
# Keyword Generation
|
40 |
query_prompt = PromptTemplate(
|
41 |
template="""
|
42 |
|
|
|
58 |
""",
|
59 |
input_variables=["question"],
|
60 |
)
|
61 |
+
# Keyword Chain
|
|
|
62 |
query_chain = query_prompt | llama3_json | JsonOutputParser()
|
63 |
|
64 |
# Google Search and Firecrawl Setup
|
|
|
71 |
scraped_data.append(scrape_response)
|
72 |
return scraped_data
|
73 |
|
74 |
+
# Summarizer
|
75 |
summarize_prompt = PromptTemplate(
|
76 |
template="""
|
77 |
|
|
|
91 |
""",
|
92 |
input_variables=["question"],
|
93 |
)
|
94 |
+
# Summarizer Chain
|
|
|
95 |
summarize_chain = summarize_prompt | llama3 | StrOutputParser()
|
96 |
|
97 |
+
# Fact Checker
|
98 |
generate_prompt = PromptTemplate(
|
99 |
template="""
|
100 |
|
|
|
124 |
""",
|
125 |
input_variables=["question", "context"],
|
126 |
)
|
127 |
+
# Fact Checker Chain
|
|
|
128 |
generate_chain = generate_prompt | llama3_json | JsonOutputParser()
|
129 |
|
130 |
# Full Flow Function
|
131 |
def fact_check_flow(user_question):
|
132 |
+
# Generate Keyword
|
133 |
keyword = query_chain.invoke({"question": user_question})["query"]
|
134 |
|
135 |
+
# Search & Scrape Results
|
136 |
context_data = search_and_scrape(keyword)
|
137 |
+
|
138 |
+
# Join Results
|
139 |
final_markdown = []
|
140 |
for results in context_data:
|
141 |
final_markdown.append(results['markdown'])
|
142 |
|
143 |
final_markdown = ' '.join(final_markdown)
|
144 |
|
145 |
+
# Summarize Results
|
146 |
context = summarize_chain.invoke({"question": final_markdown})
|
147 |
|
148 |
+
# Use Information + Context to Fact Check
|
|
|
149 |
final_response = generate_chain.invoke({"question": user_question, "context": context})
|
150 |
|
151 |
+
# Process Output
|
152 |
verdict = final_response['system_verdict']
|
153 |
explanation = final_response['explanation']
|
154 |
|
|
|
161 |
|
162 |
return verdict_html + explanation_html
|
163 |
|
|
|
|
|
|
|
|
|
|
|
164 |
demo = gr.Interface(
|
165 |
fn=fact_check_flow,
|
166 |
inputs=gr.Textbox(label="Input any information you want to fact-check!"),
|