shaheerawan3 commited on
Commit
5acf210
·
verified ·
1 Parent(s): 8e4385e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +173 -87
app.py CHANGED
@@ -1,16 +1,53 @@
1
  import streamlit as st
2
  import pandas as pd
3
  import requests
4
- import textblob
5
  from transformers import pipeline
 
 
 
 
 
6
  from nltk.tokenize import sent_tokenize
7
  import numpy as np
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  class AIOutputAnalyzer:
10
  def __init__(self):
11
- self.sentiment_analyzer = pipeline("sentiment-analysis")
12
- self.qa_pipeline = pipeline("question-answering")
 
 
 
 
13
 
 
 
 
 
 
 
 
14
  def check_bias(self, text):
15
  """Analyze text for potential bias using sentiment and keyword analysis"""
16
  results = {
@@ -19,117 +56,166 @@ class AIOutputAnalyzer:
19
  'balanced_perspective': 0
20
  }
21
 
22
- # Analyze sentiment of each sentence
23
- sentences = sent_tokenize(text)
24
- sentiments = [self.sentiment_analyzer(sent)[0] for sent in sentences]
25
-
26
- # Check for emotional language
27
- strong_sentiments = sum(1 for s in sentiments if abs(float(s['score'])) > 0.8)
28
- results['emotional_language'] = strong_sentiments / len(sentences)
29
-
30
- # Analyze subjectivity using TextBlob
31
- blob = textblob.TextBlob(text)
32
- results['subjective_statements'] = blob.subjectivity
33
-
34
- # Calculate overall bias score
35
- results['balanced_perspective'] = 1 - ((results['emotional_language'] +
36
- results['subjective_statements']) / 2)
37
-
 
 
 
 
 
 
 
 
38
  return results
39
 
40
  def verify_factual_claims(self, text):
41
- """Extract and attempt to verify factual claims in the text"""
42
- sentences = sent_tokenize(text)
43
- claims = []
44
-
45
- for sentence in sentences:
46
- if any(keyword in sentence.lower() for keyword in
47
- ['is', 'are', 'was', 'were', 'will', 'has', 'have']):
48
- claims.append({
49
- 'claim': sentence,
50
- 'confidence': self._assess_claim_confidence(sentence),
51
- 'needs_verification': True
52
- })
53
-
54
- return claims
55
-
56
- def _assess_claim_confidence(self, claim):
57
- """Assess confidence level of a claim based on language patterns"""
58
- confidence = 0.5 # Base confidence
59
-
60
- # Reduce confidence for uncertain language
61
- uncertainty_markers = ['might', 'maybe', 'possibly', 'could', 'perhaps']
62
- if any(marker in claim.lower() for marker in uncertainty_markers):
63
- confidence -= 0.2
64
 
65
- # Increase confidence for citations or specific numbers
66
- if any(char.isdigit() for char in claim) or '[' in claim:
67
- confidence += 0.2
 
 
 
 
 
 
 
 
 
68
 
69
- return min(1.0, max(0.0, confidence))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
  def main():
 
 
 
72
  st.title("AI Output Accountability Checker")
73
  st.markdown("""
74
- ### Analyze AI-generated content for:
75
- - Bias and subjectivity
76
- - Factual claims verification
77
- - Source credibility
78
  """)
79
 
 
 
 
 
80
  # Input section
81
- ai_output = st.text_area("Paste AI-generated content here:", height=200)
 
 
 
 
 
 
 
 
 
 
 
82
 
83
  if st.button("Analyze"):
84
  if ai_output:
85
  analyzer = AIOutputAnalyzer()
86
 
87
- # Progress bar
88
  progress_bar = st.progress(0)
 
89
 
90
  # Bias Analysis
91
- st.subheader("Bias Analysis")
92
- with st.spinner("Analyzing bias..."):
93
- bias_results = analyzer.check_bias(ai_output)
94
- progress_bar.progress(0.33)
95
-
96
- col1, col2, col3 = st.columns(3)
97
- col1.metric("Emotional Language",
98
- f"{bias_results['emotional_language']:.2%}")
99
- col2.metric("Subjective Content",
100
- f"{bias_results['subjective_statements']:.2%}")
101
- col3.metric("Balance Score",
102
- f"{bias_results['balanced_perspective']:.2%}")
103
 
104
- # Factual Claims
105
- st.subheader("Factual Claims Analysis")
106
- with st.spinner("Analyzing claims..."):
107
- claims = analyzer.verify_factual_claims(ai_output)
108
- progress_bar.progress(0.66)
 
 
 
 
 
 
 
 
109
 
 
 
110
  for claim in claims:
111
  with st.expander(f"Claim (Confidence: {claim['confidence']:.2%})"):
112
  st.write(claim['claim'])
 
113
  if claim['needs_verification']:
114
- st.warning("⚠️ This claim requires verification")
115
-
116
- # Overall Assessment
117
- st.subheader("Overall Assessment")
118
- progress_bar.progress(1.0)
119
-
120
- overall_score = (bias_results['balanced_perspective'] +
121
- np.mean([c['confidence'] for c in claims])) / 2
122
-
123
- st.markdown(f"""
124
- ### Reliability Score: {overall_score:.2%}
125
-
126
- **Recommendations:**
127
- - {'✅ Content appears relatively unbiased' if bias_results['balanced_perspective'] > 0.7
128
- else '⚠️ Content shows potential bias'}
129
- - {'✅ Claims are well-supported' if np.mean([c['confidence'] for c in claims]) > 0.7
130
- else '⚠️ Some claims need verification'}
131
- """)
132
 
 
 
 
 
 
 
 
 
 
 
133
  else:
134
  st.warning("Please enter some text to analyze.")
135
 
 
1
  import streamlit as st
2
  import pandas as pd
3
  import requests
4
+ from textblob import TextBlob
5
  from transformers import pipeline
6
+ import nltk
7
+ try:
8
+ nltk.data.find('tokenizers/punkt')
9
+ except LookupError:
10
+ nltk.download('punkt')
11
  from nltk.tokenize import sent_tokenize
12
  import numpy as np
13
+ import json
14
+ from urllib.parse import quote
15
+
16
+ class FactChecker:
17
+ def __init__(self):
18
+ self.google_fact_check_api = "https://factchecktools.googleapis.com/v1alpha1/claims:search"
19
+
20
+ def check_claim(self, claim_text):
21
+ """Use free fact checking APIs to verify claims"""
22
+ # Using WikiMedia API for basic fact verification
23
+ wiki_api = f"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch={quote(claim_text)}&format=json"
24
+ try:
25
+ response = requests.get(wiki_api)
26
+ results = response.json()
27
+ return {
28
+ 'found_matches': len(results.get('query', {}).get('search', [])) > 0,
29
+ 'confidence': min(len(results.get('query', {}).get('search', [])) / 5.0, 1.0),
30
+ 'sources': ['Wikipedia']
31
+ }
32
+ except:
33
+ return {'found_matches': False, 'confidence': 0.0, 'sources': []}
34
 
35
  class AIOutputAnalyzer:
36
  def __init__(self):
37
+ try:
38
+ self.sentiment_analyzer = pipeline("sentiment-analysis")
39
+ except:
40
+ st.error("Error loading sentiment analyzer. Using fallback method.")
41
+ self.sentiment_analyzer = None
42
+ self.fact_checker = FactChecker()
43
 
44
+ def analyze_sentiment_fallback(self, text):
45
+ """Fallback method using TextBlob for sentiment analysis"""
46
+ blob = TextBlob(text)
47
+ polarity = blob.sentiment.polarity
48
+ return [{'label': 'POSITIVE' if polarity > 0 else 'NEGATIVE',
49
+ 'score': abs(polarity)}]
50
+
51
  def check_bias(self, text):
52
  """Analyze text for potential bias using sentiment and keyword analysis"""
53
  results = {
 
56
  'balanced_perspective': 0
57
  }
58
 
59
+ try:
60
+ # Analyze sentiment of each sentence
61
+ sentences = sent_tokenize(text)
62
+ if self.sentiment_analyzer:
63
+ sentiments = [self.sentiment_analyzer(sent)[0] for sent in sentences]
64
+ else:
65
+ sentiments = [self.analyze_sentiment_fallback(sent)[0] for sent in sentences]
66
+
67
+ # Check for emotional language
68
+ strong_sentiments = sum(1 for s in sentiments if abs(float(s['score'])) > 0.8)
69
+ results['emotional_language'] = strong_sentiments / len(sentences)
70
+
71
+ # Analyze subjectivity
72
+ blob = TextBlob(text)
73
+ results['subjective_statements'] = blob.sentiment.subjectivity
74
+
75
+ # Calculate overall bias score
76
+ results['balanced_perspective'] = 1 - ((results['emotional_language'] +
77
+ results['subjective_statements']) / 2)
78
+ except Exception as e:
79
+ st.error(f"Error in bias analysis: {str(e)}")
80
+ results = {'emotional_language': 0, 'subjective_statements': 0,
81
+ 'balanced_perspective': 0.5}
82
+
83
  return results
84
 
85
  def verify_factual_claims(self, text):
86
+ """Extract and verify factual claims in the text"""
87
+ try:
88
+ sentences = sent_tokenize(text)
89
+ claims = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
+ for sentence in sentences:
92
+ if any(keyword in sentence.lower() for keyword in
93
+ ['is', 'are', 'was', 'were', 'will', 'has', 'have']):
94
+ # Check facts using external APIs
95
+ fact_check_result = self.fact_checker.check_claim(sentence)
96
+
97
+ claims.append({
98
+ 'claim': sentence,
99
+ 'confidence': fact_check_result['confidence'],
100
+ 'needs_verification': not fact_check_result['found_matches'],
101
+ 'sources': fact_check_result['sources']
102
+ })
103
 
104
+ return claims
105
+ except Exception as e:
106
+ st.error(f"Error in claim verification: {str(e)}")
107
+ return []
108
+
109
+ def create_report(text, bias_results, claims):
110
+ """Generate a detailed analysis report"""
111
+ report = f"""
112
+ # AI Output Analysis Report
113
+
114
+ ## Content Overview
115
+ Length: {len(text)} characters
116
+ Sentences analyzed: {len(sent_tokenize(text))}
117
+
118
+ ## Bias Analysis
119
+ - Emotional Language Score: {bias_results['emotional_language']:.2%}
120
+ - Subjective Content Score: {bias_results['subjective_statements']:.2%}
121
+ - Balance Score: {bias_results['balanced_perspective']:.2%}
122
+
123
+ ## Factual Claims Analysis
124
+ Total claims analyzed: {len(claims)}
125
+
126
+ ### Detailed Claims Breakdown:
127
+ """
128
+
129
+ for i, claim in enumerate(claims, 1):
130
+ report += f"""
131
+ Claim {i}:
132
+ - Statement: "{claim['claim']}"
133
+ - Confidence: {claim['confidence']:.2%}
134
+ - Status: {"Verified" if not claim['needs_verification'] else "Needs Verification"}
135
+ - Sources: {', '.join(claim['sources'])}
136
+ """
137
+
138
+ return report
139
 
140
  def main():
141
+ st.set_page_config(page_title="AI Output Accountability Checker",
142
+ layout="wide")
143
+
144
  st.title("AI Output Accountability Checker")
145
  st.markdown("""
146
+ ### Analyze AI-generated content for bias, facts, and credibility
147
+ Upload or paste AI-generated content to get a comprehensive analysis.
 
 
148
  """)
149
 
150
+ # Sidebar for language selection
151
+ languages = ["English", "Spanish", "French", "German"]
152
+ selected_language = st.sidebar.selectbox("Select Language", languages)
153
+
154
  # Input section
155
+ input_method = st.radio("Choose input method:",
156
+ ["Text Input", "File Upload"])
157
+
158
+ if input_method == "File Upload":
159
+ uploaded_file = st.file_uploader("Upload a text file", type=['txt'])
160
+ if uploaded_file:
161
+ ai_output = uploaded_file.read().decode()
162
+ else:
163
+ ai_output = ""
164
+ else:
165
+ ai_output = st.text_area("Paste AI-generated content here:",
166
+ height=200)
167
 
168
  if st.button("Analyze"):
169
  if ai_output:
170
  analyzer = AIOutputAnalyzer()
171
 
172
+ # Analysis with progress tracking
173
  progress_bar = st.progress(0)
174
+ status_text = st.empty()
175
 
176
  # Bias Analysis
177
+ status_text.text("Analyzing bias...")
178
+ bias_results = analyzer.check_bias(ai_output)
179
+ progress_bar.progress(0.33)
180
+
181
+ # Claims Analysis
182
+ status_text.text("Verifying claims...")
183
+ claims = analyzer.verify_factual_claims(ai_output)
184
+ progress_bar.progress(0.66)
 
 
 
 
185
 
186
+ # Generate Report
187
+ status_text.text("Generating report...")
188
+ report = create_report(ai_output, bias_results, claims)
189
+ progress_bar.progress(1.0)
190
+ status_text.text("Analysis complete!")
191
+
192
+ # Display Results
193
+ col1, col2 = st.columns(2)
194
+
195
+ with col1:
196
+ st.subheader("Quick Analysis")
197
+ st.metric("Overall Reliability",
198
+ f"{(bias_results['balanced_perspective'] + np.mean([c['confidence'] for c in claims])) / 2:.2%}")
199
 
200
+ # Interactive claims analysis
201
+ st.subheader("Claims Analysis")
202
  for claim in claims:
203
  with st.expander(f"Claim (Confidence: {claim['confidence']:.2%})"):
204
  st.write(claim['claim'])
205
+ st.write(f"Sources: {', '.join(claim['sources'])}")
206
  if claim['needs_verification']:
207
+ st.warning("⚠️ This claim needs additional verification")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
 
209
+ with col2:
210
+ st.subheader("Detailed Report")
211
+ st.download_button(
212
+ label="Download Report",
213
+ data=report,
214
+ file_name="ai_analysis_report.md",
215
+ mime="text/markdown"
216
+ )
217
+ st.markdown(report)
218
+
219
  else:
220
  st.warning("Please enter some text to analyze.")
221