shikharyashmaurya's picture
Update app.py
065e984 verified
import streamlit as st
import google.generativeai as genai
import ast
import time
import os
import re
st.title('Verified Interseting fact geneartor')
secret_key = os.getenv("SECRET_KEY")
genai.configure(api_key=secret_key)
def extract_python_code(text):
pattern = r"```python\n(.*?)```"
match = re.search(pattern, text, re.DOTALL)
if match:
return match.group(1).strip()
else:
return None
def get_gemini_response(input):
prompt2='''you are an fact checker,you will get an text.
you will respond with two thing-
1.true false or unsure(if you are unsure or knowledge cutoff)
2.evidence in support or knowledge cutoff
you will respond in this format -
['true','false','unsure'],['evidence','knowledge cut off']
example-'dbhfv company founded in 2024'
response-['Unsure','knowledge cut off']
Now give response in the exact described format of the following text - '''
old=prompt2+input
model = genai.GenerativeModel('gemini-1.5-flash')
response1 = model.generate_content(old)
return response1.text
text=st.text_input('Write something to generate interseting facts. Like - Elephant , Bears')
prompt1='''Your work is to generate interseting facts.
important - generate fact and append it in a python list.
example-
text-india
your response-['india got independence in 1947','india first prime minister was nehru']
text - earth
your response-['earth look blue from space','earth is 3 planet in our solar system']
now generate upto 10 intersesting fact for the later text in correct specified format
text - '''
def check(x):
try:
y=ast.literal_eval(x)
return y
except:
y=''
st.warning('rerun code not splitted correctly')
return y
submit=st.button('submit')
if text and submit:
try:
new = prompt1 + text
model = genai.GenerativeModel('gemini-1.5-flash')
response = model.generate_content(new)
a = response.text
b = extract_python_code(a)
my_list = check(b)
if isinstance(my_list, list):
for i in my_list:
c = get_gemini_response(i)
new_list = check(c)
if new_list[0] == 'True':
st.write(i)
st.write(c)
time.sleep(2)
else:
st.warning('Error in checking.If error remains reload the page')
except Exception as e:
st.error(f"Error: {e}")
st.write("Please try again!If error remains reload the page")