Spaces:
Sleeping
Sleeping
| # =============================== | |
| # Fake/Real News Detection App | |
| # Supports English and Bangla | |
| # =============================== | |
| import gradio as gr | |
| from transformers import pipeline | |
| import matplotlib.pyplot as plt | |
| import io | |
| import base64 | |
| # Load the model (Hugging Face) | |
| # Make sure you have internet and the model exists on HF | |
| classifier = pipeline( | |
| "text-classification", | |
| model="mrm8488/bert-tiny-finetuned-fake-news-detection", | |
| return_all_scores=True | |
| ) | |
| def predict_news(text): | |
| if not text.strip(): | |
| return "<span style='color:red;'>Please enter news text!</span>", None | |
| # Get prediction | |
| results = classifier(text)[0] # List of dicts with label & score | |
| # Sort to get highest probability | |
| results = sorted(results, key=lambda x: x['score'], reverse=True) | |
| label = results[0]['label'] | |
| score = results[0]['score'] | |
| percent = round(score * 100, 2) | |
| # Color coding | |
| if label.lower() == "real": | |
| output_text = f"<span style='color:green;'>Real {percent}%</span>" | |
| else: | |
| output_text = f"<span style='color:red;'>Fake {percent}%</span>" | |
| # Generate chart | |
| labels = [r['label'] for r in results] | |
| scores = [r['score']*100 for r in results] | |
| colors = ['green' if l.lower() == 'real' else 'red' for l in labels] | |
| fig, ax = plt.subplots(figsize=(4,2)) | |
| ax.bar(labels, scores, color=colors) | |
| ax.set_ylim([0, 100]) | |
| ax.set_ylabel('Confidence (%)') | |
| ax.set_title('Prediction Confidence') | |
| # Convert chart to image for Gradio | |
| buf = io.BytesIO() | |
| plt.tight_layout() | |
| plt.savefig(buf, format='png') | |
| buf.seek(0) | |
| chart_base64 = base64.b64encode(buf.getvalue()).decode("utf-8") | |
| chart_html = f'<img src="data:image/png;base64,{chart_base64}"/>' | |
| return output_text, chart_html | |
| # Gradio Interface | |
| app = gr.Interface( | |
| fn=predict_news, | |
| inputs=gr.Textbox(lines=6, placeholder="Enter news text in English or Bangla..."), | |
| outputs=[ | |
| gr.HTML(label="Prediction"), | |
| gr.HTML(label="Confidence Chart") | |
| ], | |
| title="Fake/Real News Detection", | |
| description="Enter news text in English or Bangla and see if it's Fake or Real with confidence." | |
| ) | |
| # Launch the app | |
| app.launch() | |