Zeamays3427 commited on
Commit
8ecc298
Β·
verified Β·
1 Parent(s): 2abcd65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -23
app.py CHANGED
@@ -106,22 +106,16 @@ def predict_and_explain(title, text):
106
  suggestions = generate_suggestions(title, text, keywords)
107
  return f"""
108
  **Prediction**: Fake News
109
-
110
  **Probability**: {fake_prob:.2f}% Fake, {real_prob:.2f}% Real
111
-
112
  **Keywords**: {', '.join(keywords)}
113
-
114
  **Suggestions**:
115
-
116
  {suggestions}
117
  """
118
  else:
119
  # If the news is real, just show the prediction and keywords
120
  return f"""
121
  **Prediction**: Real News
122
-
123
  **Probability**: {real_prob:.2f}% Real, {fake_prob:.2f}% Fake
124
-
125
  **Keywords**: {', '.join(keywords)}
126
  """
127
 
@@ -131,18 +125,15 @@ def generate_suggestions(title, text, keywords):
131
  prompt = f"""
132
  You are a specialist in fact-checking. Based on the title, text, and keywords of the fake news,
133
  please suggest some ways to know more about the facts. Please give recommendations that are easy to accept.
134
-
135
  Keywords: {', '.join(keywords)}
136
-
137
  Title: {title}
138
-
139
  Text: {text}
140
  """
141
 
142
  try:
143
  # Use OpenAI GPT-4 API to generate suggestions using chat completion
144
  response = client.chat.completions.create(
145
- model="gpt-4o-2024-08-06", # Use the GPT-4 model
146
  messages=[
147
  {"role": "system", "content": "You are a helpful assistant specialized in fact-checking."},
148
  {"role": "user", "content": prompt} # Pass the constructed prompt as user input
@@ -161,18 +152,110 @@ def generate_suggestions(title, text, keywords):
161
 
162
  return suggestions
163
 
164
- # Gradio interface setup
165
- iface = gr.Interface(
166
- fn=predict_and_explain, # The function to handle user input and return predictions
167
- inputs=[
168
- gr.Textbox(label="Title"), # Textbox for the news title
169
- gr.Textbox(label="Text", lines=10) # Textbox for the news content
170
- ],
171
- outputs="markdown", # Output format is markdown
172
- title="Fake News Debunker", # Title of the Gradio app
173
- description="Enter the news title and content to check if it's fake. If fake, get suggestions on how to know more about the facts.",
174
- # Description of the app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
  )
176
 
177
- # Launch the Gradio app
178
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  suggestions = generate_suggestions(title, text, keywords)
107
  return f"""
108
  **Prediction**: Fake News
 
109
  **Probability**: {fake_prob:.2f}% Fake, {real_prob:.2f}% Real
 
110
  **Keywords**: {', '.join(keywords)}
 
111
  **Suggestions**:
 
112
  {suggestions}
113
  """
114
  else:
115
  # If the news is real, just show the prediction and keywords
116
  return f"""
117
  **Prediction**: Real News
 
118
  **Probability**: {real_prob:.2f}% Real, {fake_prob:.2f}% Fake
 
119
  **Keywords**: {', '.join(keywords)}
120
  """
121
 
 
125
  prompt = f"""
126
  You are a specialist in fact-checking. Based on the title, text, and keywords of the fake news,
127
  please suggest some ways to know more about the facts. Please give recommendations that are easy to accept.
 
128
  Keywords: {', '.join(keywords)}
 
129
  Title: {title}
 
130
  Text: {text}
131
  """
132
 
133
  try:
134
  # Use OpenAI GPT-4 API to generate suggestions using chat completion
135
  response = client.chat.completions.create(
136
+ model="gpt-4", # Use the GPT-4 model
137
  messages=[
138
  {"role": "system", "content": "You are a helpful assistant specialized in fact-checking."},
139
  {"role": "user", "content": prompt} # Pass the constructed prompt as user input
 
152
 
153
  return suggestions
154
 
155
+ # Custom CSS styles
156
+ custom_css = """
157
+ .gr-interface {
158
+ background-color: #f8f9fa;
159
+ }
160
+ .gr-form {
161
+ background-color: white;
162
+ padding: 2rem;
163
+ border-radius: 1rem;
164
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
165
+ }
166
+ .gr-input {
167
+ border: 2px solid #e9ecef;
168
+ border-radius: 0.5rem;
169
+ transition: border-color 0.3s ease;
170
+ }
171
+ .gr-input:focus {
172
+ border-color: #4a90e2;
173
+ box-shadow: 0 0 0 2px rgba(74, 144, 226, 0.2);
174
+ }
175
+ .gr-button {
176
+ background-color: #4a90e2;
177
+ border: none;
178
+ border-radius: 0.5rem;
179
+ color: white;
180
+ transition: background-color 0.3s ease;
181
+ }
182
+ .gr-button:hover {
183
+ background-color: #357abd;
184
+ }
185
+ .footer {
186
+ text-align: center;
187
+ margin-top: 2rem;
188
+ color: #6c757d;
189
+ }
190
+ """
191
+
192
+ # Create custom theme
193
+ theme = gr.themes.Soft(
194
+ primary_hue="blue",
195
+ secondary_hue="gray",
196
+ neutral_hue="gray",
197
+ spacing_size=gr.themes.sizes.spacing_lg,
198
+ radius_size=gr.themes.sizes.radius_lg,
199
+ font=[gr.themes.GoogleFont("Inter"), "system-ui", "sans-serif"]
200
+ ).set(
201
+ body_background_fill="#f8f9fa",
202
+ body_background_fill_dark="#1a1b1e",
203
+ button_primary_background_fill="#4a90e2",
204
+ button_primary_background_fill_hover="#357abd",
205
+ button_primary_text_color="white",
206
+ input_background_fill="white",
207
+ input_border_width="2px",
208
+ input_shadow="0 2px 4px rgba(0,0,0,0.05)",
209
  )
210
 
211
+ # Gradio interface setup
212
+ with gr.Blocks(theme=theme, css=custom_css) as iface:
213
+ gr.Markdown(
214
+ """
215
+ # πŸ” Fake News Detection & Analysis System
216
+
217
+ ### Your Tool for Identifying Misinformation and Finding Facts
218
+
219
+ Enter a news article's title and content below to:
220
+ - Analyze the authenticity of the news
221
+ - Extract key topics and themes
222
+ - Get fact-checking recommendations
223
+ """
224
+ )
225
+
226
+ with gr.Row():
227
+ with gr.Column():
228
+ title_input = gr.Textbox(
229
+ label="πŸ“° News Title",
230
+ placeholder="Enter the news title here...",
231
+ lines=1
232
+ )
233
+ text_input = gr.Textbox(
234
+ label="πŸ“ News Content",
235
+ placeholder="Enter the news content here...",
236
+ lines=10
237
+ )
238
+ submit_btn = gr.Button("Analyze Now πŸ”", variant="primary")
239
+
240
+ output = gr.Markdown(label="Analysis Results")
241
+
242
+ # Set submit button action
243
+ submit_btn.click(
244
+ fn=predict_and_explain,
245
+ inputs=[title_input, text_input],
246
+ outputs=output,
247
+ )
248
+
249
+ # Add footer
250
+ gr.Markdown(
251
+ """
252
+ <div class="footer">
253
+ πŸ’‘ Note: This system uses advanced AI models for analysis. Results should be used as a reference.
254
+ Always maintain critical thinking and independent judgment.
255
+ </div>
256
+ """,
257
+ visible=True
258
+ )
259
+
260
+ # Launch the application
261
+ iface.launch()