shikharyashmaurya commited on
Commit
0d2ecfb
1 Parent(s): 39054ed

Upload app4.py

Browse files
Files changed (1) hide show
  1. app4.py +112 -0
app4.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import google.generativeai as genai
3
+ import ast
4
+ import time
5
+ import re
6
+ import os
7
+ from typing import List, Tuple, Optional
8
+
9
+ def extract_python_code(text: str) -> Optional[str]:
10
+ pattern = r"```python\n(.*?)```"
11
+ match = re.search(pattern, text, re.DOTALL)
12
+ return match.group(1).strip() if match else None
13
+
14
+ def configure_genai():
15
+ secret_key = os.getenv("SECRET_KEY")
16
+ if not secret_key:
17
+ st.error("API key not found. Please set the SECRET_KEY environment variable.")
18
+ st.stop()
19
+ genai.configure(api_key=secret_key)
20
+
21
+ def parse_gemini_response(response_text: str) -> Tuple[str, str]:
22
+ try:
23
+ # First, try to parse as a single list
24
+ parsed = ast.literal_eval(response_text)
25
+ if isinstance(parsed, list) and len(parsed) == 2:
26
+ return parsed[0], parsed[1]
27
+
28
+ # If that fails, look for multiple lists
29
+ matches = re.findall(r'\[.*?\]', response_text)
30
+ if len(matches) >= 2:
31
+ first_list = ast.literal_eval(matches[0])
32
+ second_list = ast.literal_eval(matches[1])
33
+ return first_list[0], second_list[0]
34
+
35
+ # If no valid format is found, raise an exception
36
+ raise ValueError("Unexpected response format")
37
+ except Exception as e:
38
+ return "Error", f"Failed to parse response: {str(e)}"
39
+
40
+ def get_gemini_response(input_text: str) -> Tuple[str, str]:
41
+ prompt = """You are a fact checker. Given a text, respond with:
42
+ 1. 'true', 'false', or 'unsure' (if you are unsure or knowledge cutoff)
43
+ 2. Evidence in support or 'knowledge cutoff'
44
+
45
+ Respond in this exact format: ['true/false/unsure', 'evidence or knowledge cutoff']
46
+ Example input: 'Google was founded in 1998'
47
+ Example output: ['true', 'Google was indeed founded in September 1998 by Larry Page and Sergey Brin']
48
+
49
+ Now give a response in the exact described format for the following text:
50
+ """
51
+ model = genai.GenerativeModel('gemini-1.5-pro')
52
+ try:
53
+ response = model.generate_content(prompt + input_text)
54
+ result, evidence = parse_gemini_response(response.text)
55
+ return result, evidence
56
+ except Exception as e:
57
+ return "Error", f"Failed to get or parse the model's response: {str(e)}"
58
+
59
+ def break_down_text(text: str) -> List[str]:
60
+ prompt = """Break down the following text into a list of individual factual statements that can be independently verified. Return only a Python list of strings.
61
+
62
+ Example input: "The Eiffel Tower, built in 1889, is 324 meters tall and located in Paris, France."
63
+ Example output: ["The Eiffel Tower was built in 1889", "The Eiffel Tower is 324 meters tall", "The Eiffel Tower is located in Paris, France"]
64
+
65
+ Now break down the following text:
66
+ """
67
+ model = genai.GenerativeModel('gemini-1.5-pro')
68
+ response = model.generate_content(prompt + text)
69
+
70
+ code = extract_python_code(response.text)
71
+ try:
72
+ return ast.literal_eval(code) if code else []
73
+ except (ValueError, SyntaxError):
74
+ st.error(f"Failed to parse the breakdown response: {response.text}")
75
+ return []
76
+
77
+ def main():
78
+ st.title("Fact Checker")
79
+ configure_genai()
80
+
81
+ text = st.text_area('Paste the text to fact check (preferably about facts before 2021)', height=150)
82
+
83
+ if st.button("Check Facts"):
84
+ if not text:
85
+ st.warning("Please enter some text to fact-check.")
86
+ return
87
+
88
+ statements = break_down_text(text)
89
+
90
+ if not statements:
91
+ st.error("Failed to break down the text into checkable statements. Please try rephrasing your input.")
92
+ return
93
+
94
+ st.subheader("Fact Checking Results:")
95
+ for statement in statements:
96
+ with st.expander(statement):
97
+ with st.spinner('Checking...'):
98
+ result, evidence = get_gemini_response(statement)
99
+
100
+ if result.lower() == "true":
101
+ st.success(f"Likely True: {evidence}")
102
+ elif result.lower() == "false":
103
+ st.error(f"Likely False: {evidence}")
104
+ elif result.lower() == "unsure":
105
+ st.warning(f"Uncertain: {evidence}")
106
+ else:
107
+ st.error(f"Error in fact-checking: {evidence}")
108
+
109
+ time.sleep(3) # Delay to avoid rate limiting
110
+
111
+ if __name__ == "__main__":
112
+ main()