Spaces:
Runtime error
Runtime error
| # import streamlit as st | |
| # import google.generativeai as genai | |
| # import ast | |
| # import time | |
| # import re | |
| # 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 | |
| # import os | |
| # secret_key = os.getenv("SECRET_KEY") | |
| # genai.configure(api_key=secret_key) | |
| # 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) | |
| # 2.evidence in support | |
| # you will respond in this format - | |
| # ['true','false','unsure'],['evidence','other'] | |
| # example-'google company founded in 2024' | |
| # response-['false','Google was officially launched in 1998'] | |
| # 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 | |
| # st.title("Fact Checker") | |
| # text=st.text_input('paste the text to fact check.Ask facts before that of 2021') | |
| # prompt1='''impoprtant - give answer only in python list format. | |
| # you will get an long paragraph of text. | |
| # But the long text is diffcult to fact check in google. | |
| # Your work is to break down the long text into small pieces so that it can be checked independently. | |
| # You will break down texts into list elements. | |
| # example-'india got independence in 1947 its first prime minister was nehru'. | |
| # response-['india got independence in 1947','india first prime minister was nehru'] | |
| # impoprtant - give answer only in python list format | |
| # Now you will give me a response of the given following text in the described format as a python list- ''' | |
| # if text: | |
| # new=prompt1+text | |
| # model=genai.GenerativeModel('gemini-1.5-flash') | |
| # response=model.generate_content(new) | |
| # a=response.text | |
| # b=extract_python_code(a) | |
| # try: | |
| # my_list = ast.literal_eval(b) | |
| # except: | |
| # my_list='' | |
| # st.warning('rerun code') | |
| # if isinstance(my_list,list): | |
| # for i in my_list: | |
| # c=get_gemini_response(i) | |
| # st.write(i) | |
| # st.write(c) | |
| # time.sleep(2) | |
| # else: | |
| # st.warning('rerun code') | |
| import streamlit as st | |
| import ast | |
| import time | |
| import re | |
| import os | |
| from groq import Groq | |
| # Helper to extract Python code block | |
| 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 | |
| # Configure Groq | |
| api_key = os.getenv("SECRET_KEY") | |
| client = Groq(api_key=api_key) | |
| # Function to call Groq API | |
| def query_groq(prompt, model_name="gemma2-9b-it"): | |
| chat_completion = client.chat.completions.create( | |
| messages=[{"role": "user", "content": prompt}], | |
| model=model_name | |
| ) | |
| return chat_completion.choices[0].message.content | |
| # Fact checking logic | |
| def get_fact_check_response(input_text): | |
| prompt2 = '''you are a fact checker, you will get a text. | |
| you will respond with two things: | |
| 1. true, false, or unsure (if you are unsure) | |
| 2. evidence in support | |
| You will respond in this format: | |
| ['true','false','unsure'],['evidence','other'] | |
| Example: | |
| Input: 'google company founded in 2024' | |
| Response: ['false','Google was officially launched in 1998'] | |
| Now give response in the exact described format of the following text - ''' | |
| full_prompt = prompt2 + input_text | |
| return query_groq(full_prompt) | |
| # Splitting paragraph into small factual units | |
| def split_into_facts(paragraph): | |
| prompt1 = '''important - give answer only in python list format. | |
| you will get a long paragraph of text. | |
| But the long text is difficult to fact check in google. | |
| Your work is to break down the long text into small pieces so that it can be checked independently. | |
| You will break down text into a large number of list elements. | |
| Example: | |
| Input: 'india got independence in 1947 its first prime minister was nehru' | |
| Response: ['india got independence in 1947','india first prime minister was nehru'] | |
| important - give answer only in python list format | |
| Now you will give me a response of the given following text in the described format as a python list - ''' | |
| full_prompt = prompt1 + paragraph | |
| return query_groq(full_prompt) | |
| # Streamlit UI | |
| st.title("Fact Checker using Groq") | |
| text = st.text_input('Paste the text to fact-check (ask facts before 2021)') | |
| if text: | |
| response = split_into_facts(text) | |
| code_block = extract_python_code(response) | |
| try: | |
| fact_list = ast.literal_eval(code_block) | |
| except: | |
| fact_list = '' | |
| st.warning('⚠️ Could not parse list. Try rerunning.') | |
| if isinstance(fact_list, list): | |
| for fact in fact_list: | |
| result = get_fact_check_response(fact) | |
| st.write(f"**Fact:** {fact}") | |
| st.write(result) | |
| time.sleep(2) | |
| else: | |
| st.warning('⚠️ Invalid response format. Try rerunning.') | |