Spaces:
Runtime error
Runtime error
Update app.py
Browse files2 unmodified lines34567899 unmodified lines192021222324252627282921 unmodified lines5152535455565758596061626364154 unmodified lines21922022122222322422522622722822923023123295 unmodified lines3283293303313323333343353363373382 unmodified linesimport plotly.graph_objects as goimport streamlit as stimport jsonimport google.generativeai as genaiimport concurrent.futuresimport textstatimport trafilatura9 unmodified linesURL_B = "https://edition.cnn.com/2026/03/30/world/live-news/iran-war-us-israel-trump"# Initialize the AI modelGEMINI_API_KEY = st.secrets.get("GEMINI_API_KEY")if GEMINI_API_KEY:genai.configure(api_key=GEMINI_API_KEY)ai_model = genai.GenerativeModel('gemini-3.1-flash-lite-preview')def _truncate_to_words(text: str, limit: int) -> str:"""Truncates text by word count."""21 unmodified linesText to analyze:"{safe_text}""""response = ai_model.generate_content(prompt,generation_config={"response_mime_type": "application/json","temperature": 0.1,})llm_data = json.loads(response.text)raw_reading_ease = textstat.flesch_reading_ease(safe_text)154 unmodified linesText 1: "{safe_a}"Text 2: "{safe_b}""""response = ai_model.generate_content(prompt,generation_config={"response_mime_type": "application/json","temperature": 0.1,})result = json.loads(response.text)return {"relationship": result.get("relationship", "NEUTRAL"), "confidence": result.get("confidence", 0.0)}95 unmodified linesst.error("One of the URLs could not be scraped. Please copy and paste the text directly.")else:with st.spinner("Analyzing framing semantics for both sources (this takes a few seconds)..."):try:st.session_state.results_a = analyze_article(text_a_clean)st.session_state.results_b = analyze_article(text_b_clean)st.session_state.nli_result = check_contradiction(text_a_clean, text_b_clean)except Exception as e:error_msg = str(e)2 unmodified lines34567899 unmodified lines1920212223242526272829303132333421 unmodified lines5657585960616263646566676869707172737475154 unmodified lines23023123223323423523623723823924024124224324424524624724824995 unmodified lines3453463473483493503513523533543553563573583593603612 unmodified linesimport plotly.graph_objects as goimport streamlit as stimport jsonfrom openai import OpenAIimport concurrent.futuresimport textstatimport trafilatura9 unmodified linesURL_B = "https://edition.cnn.com/2026/03/30/world/live-news/iran-war-us-israel-trump"# Initialize the AI modelGROQ_API_KEY = st.secrets.get("GROQ_API_KEY")if not GROQ_API_KEY:st.warning("Groq API Token Missing.")st.stop()client = OpenAI(api_key=GROQ_API_KEY,base_url="https://api.groq.com/openai/v1",)def _truncate_to_words(text: str, limit: int) -> str:"""Truncates text by word count."""21 unmodified linesText to analyze:"{safe_text}""""try:response = client.chat.completions.create(model="llama-3.3-70b-versatile",response_format={"type": "json_object"},temperature=0.1,messages=[{"role": "system", "content": "You are a media analyst. You MUST respond with ONLY valid JSON matching the exact requested schema."},{"role": "user", "content": prompt}])llm_data = json.loads(response.choices[0].message.content)except Exception as e:print(f"Error parsing LLM response: {e}")llm_data = {}raw_reading_ease = textstat.flesch_reading_ease(safe_text)154 unmodified linesText 1: "{safe_a}"Text 2: "{safe_b}""""try:response = client.chat.completions.create(model="llama-3.3-70b-versatile",response_format={"type": "json_object"},temperature=0.1,messages=[{"role": "system", "content": "You are a fact-checker. You MUST respond with ONLY valid JSON."},{"role": "user", "content": prompt}])result = json.loads(response.choices[0].message.content)except Exception as e:print(f"Error in contradiction check: {e}")result = {}return {"relationship": result.get("relationship", "NEUTRAL"), "confidence": result.get("confidence", 0.0)}95 unmodified linesst.error("One of the URLs could not be scraped. Please copy and paste the text directly.")else:with st.spinner("Analyzing both sources."):try:with concurrent.futures.ThreadPoolExecutor() as executor:future_a = executor.submit(analyze_article, text_a_clean)future_b = executor.submit(analyze_article, text_b_clean)future_nli = executor.submit(check_contradiction, text_a_clean, text_b_clean)st.session_state.results_a = future_a.result()st.session_state.results_b = future_b.result()st.session_state.nli_result = future_nli.result()except Exception as e:error_msg = str(e)
app.py+48-25