NKessler commited on
Commit
3e809a2
·
verified ·
1 Parent(s): cb65a68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -24
app.py CHANGED
@@ -44,11 +44,8 @@ def _extract_json_from_llm(response_text: str) -> dict:
44
  if match:
45
  return json.loads(match.group(0))
46
  return json.loads(response_text)
47
- except json.JSONDecodeError:
48
- return {
49
- "sentiment_score": 0.0, "primary_tone": "neutral",
50
- "primary_theme": "unclear", "tone_scores": {"neutral": 1.0}
51
- }
52
 
53
  def analyze_article(text: str) -> dict:
54
  """Analyzes framing using an LLM API and calculates readability."""
@@ -68,11 +65,8 @@ def analyze_article(text: str) -> dict:
68
  "{safe_text}"
69
  """
70
 
71
- try:
72
- response = client.text_generation(prompt, max_new_tokens=250, temperature=0.1)
73
- llm_data = _extract_json_from_llm(response)
74
- except Exception as e:
75
- llm_data = _extract_json_from_llm("") # fallback
76
 
77
  subjectivity_score = TextBlob(safe_text).sentiment.subjectivity
78
  raw_reading_ease = textstat.flesch_reading_ease(safe_text)
@@ -219,12 +213,9 @@ def check_contradiction(text_a: str, text_b: str) -> dict:
219
  Text 1: "{text_a[:1000]}"
220
  Text 2: "{text_b[:1000]}"
221
  """
222
- try:
223
- response = client.text_generation(prompt, max_new_tokens=100, temperature=0.1)
224
- result = _extract_json_from_llm(response)
225
- return {"relationship": result.get("relationship", "NEUTRAL"), "confidence": result.get("confidence", 0.0)}
226
- except:
227
- return {"relationship": "NEUTRAL", "confidence": 0.0}
228
 
229
 
230
  # USER INTERFACE
@@ -307,14 +298,20 @@ if st.button("Analyze and Compare Sources", use_container_width=True, type="prim
307
 
308
  else:
309
  with st.spinner("Analyzing framing semantics for both sources."):
310
- with concurrent.futures.ThreadPoolExecutor() as executor:
311
- future_a = executor.submit(analyze_article, text_a_clean)
312
- future_b = executor.submit(analyze_article, text_b_clean)
313
- future_nli = executor.submit(check_contradiction, text_a_clean, text_b_clean)
314
-
315
- st.session_state.results_a = future_a.result()
316
- st.session_state.results_b = future_b.result()
317
- st.session_state.nli_result = future_nli.result()
 
 
 
 
 
 
318
 
319
  # Analysis Display
320
  if st.session_state.results_a and st.session_state.results_b:
 
44
  if match:
45
  return json.loads(match.group(0))
46
  return json.loads(response_text)
47
+ except Exception as e:
48
+ raise ValueError(f"Failed to parse LLM response into JSON. Raw response: {response_text[:150]}...")
 
 
 
49
 
50
  def analyze_article(text: str) -> dict:
51
  """Analyzes framing using an LLM API and calculates readability."""
 
65
  "{safe_text}"
66
  """
67
 
68
+ response = client.text_generation(prompt, max_new_tokens=250, temperature=0.1)
69
+ llm_data = _extract_json_from_llm(response)
 
 
 
70
 
71
  subjectivity_score = TextBlob(safe_text).sentiment.subjectivity
72
  raw_reading_ease = textstat.flesch_reading_ease(safe_text)
 
213
  Text 1: "{text_a[:1000]}"
214
  Text 2: "{text_b[:1000]}"
215
  """
216
+ response = client.text_generation(prompt, max_new_tokens=100, temperature=0.1)
217
+ result = _extract_json_from_llm(response)
218
+ return {"relationship": result.get("relationship", "NEUTRAL"), "confidence": result.get("confidence", 0.0)}
 
 
 
219
 
220
 
221
  # USER INTERFACE
 
298
 
299
  else:
300
  with st.spinner("Analyzing framing semantics for both sources."):
301
+ try:
302
+ with concurrent.futures.ThreadPoolExecutor() as executor:
303
+ future_a = executor.submit(analyze_article, text_a_clean)
304
+ future_b = executor.submit(analyze_article, text_b_clean)
305
+ future_nli = executor.submit(check_contradiction, text_a_clean, text_b_clean)
306
+
307
+ st.session_state.results_a = future_a.result()
308
+ st.session_state.results_b = future_b.result()
309
+ st.session_state.nli_result = future_nli.result()
310
+ except Exception as e:
311
+ st.error(f"API or Processing Error: {str(e)}")
312
+ st.session_state.results_a = None
313
+ st.session_state.results_b = None
314
+ st.session_state.nli_result = None
315
 
316
  # Analysis Display
317
  if st.session_state.results_a and st.session_state.results_b: