Ginidu2003 commited on
Commit
d3a92b9
Β·
verified Β·
1 Parent(s): 3007158

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -29
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import gradio as gr
3
  import pandas as pd
4
  import torch
@@ -8,16 +7,15 @@ from nltk.corpus import stopwords
8
  from nltk.stem import WordNetLemmatizer
9
  import re
10
  import string
11
- import torch
12
 
13
- # ====================== PREPROCESSING ======================
14
  nltk.download('wordnet', quiet=True)
15
  nltk.download('punkt', quiet=True)
16
- nltk.download('punkt_tab', quiet=True) # ← THIS FIXES YOUR ERROR
17
-
18
 
19
  lemmatizer = WordNetLemmatizer()
20
 
 
21
  def preprocess_text(text):
22
  if not isinstance(text, str):
23
  return ""
@@ -31,24 +29,23 @@ def preprocess_text(text):
31
  return ' '.join(tokens)
32
 
33
 
34
- # ====================== LOAD YOUR FINE-TUNED MODEL ======================
35
- model_name = "Ginidu2003/Distilbert-Base-News-classifier" # ← Change if your model name is different
36
 
 
 
 
 
 
37
  @torch.no_grad()
38
  def classify_csv(file):
39
  try:
40
  df = pd.read_csv(file)
41
-
42
  if 'content' not in df.columns:
43
  return "Error: CSV must have a column named 'content'", None
44
 
45
- # Preprocess
46
  df['clean_content'] = df['content'].apply(preprocess_text)
47
 
48
- # Load classifier
49
- classifier = pipeline("text-classification", model=model_name, device=-1)
50
 
51
- # Predict
52
  predictions = []
53
  for text in df['clean_content']:
54
  if not text.strip():
@@ -60,33 +57,58 @@ def classify_csv(file):
60
  df['class'] = predictions
61
  df = df.drop(columns=['clean_content'], errors='ignore')
62
 
63
- # Save output
64
  output_file = "output.csv"
65
  df.to_csv(output_file, index=False)
66
 
67
  return f"βœ… Success! Classified {len(df)} rows", output_file
68
-
69
  except Exception as e:
70
  return f"❌ Error: {str(e)}", None
71
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  # ====================== GRADIO INTERFACE ======================
73
  with gr.Blocks(title="Daily Mirror News Classifier") as demo:
74
  gr.Markdown("# πŸ“° Daily Mirror News Classifier")
75
- gr.Markdown("### Classify news into Business, Opinion, Political Gossip, Sports, or World News")
76
-
77
- with gr.Row():
78
- file_input = gr.File(label="Upload CSV file", file_types=[".csv"])
79
-
80
- classify_btn = gr.Button("πŸš€ Classify News", variant="primary")
81
-
82
- output_text = gr.Textbox(label="Status")
83
- output_file = gr.File(label="Download output.csv")
84
-
85
- classify_btn.click(
86
- fn=classify_csv,
87
- inputs=file_input,
88
- outputs=[output_text, output_file]
89
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
  gr.Markdown("Built for Text Analytics Assignment - Section 02")
92
 
 
 
1
  import gradio as gr
2
  import pandas as pd
3
  import torch
 
7
  from nltk.stem import WordNetLemmatizer
8
  import re
9
  import string
 
10
 
11
+ # ====================== NLTK SETUP ======================
12
  nltk.download('wordnet', quiet=True)
13
  nltk.download('punkt', quiet=True)
14
+ nltk.download('punkt_tab', quiet=True)
 
15
 
16
  lemmatizer = WordNetLemmatizer()
17
 
18
+
19
  def preprocess_text(text):
20
  if not isinstance(text, str):
21
  return ""
 
29
  return ' '.join(tokens)
30
 
31
 
 
 
32
 
33
+ # ====================== MODELS ======================
34
+ classifier_model = "Ginidu2003/Distilbert-Base-News-classifier"
35
+ qa_model = "deepset/roberta-base-squad2" # Best for news Q&A
36
+
37
+ # ====================== CLASSIFICATION FUNCTION ======================
38
  @torch.no_grad()
39
  def classify_csv(file):
40
  try:
41
  df = pd.read_csv(file)
 
42
  if 'content' not in df.columns:
43
  return "Error: CSV must have a column named 'content'", None
44
 
 
45
  df['clean_content'] = df['content'].apply(preprocess_text)
46
 
47
+ classifier = pipeline("text-classification", model=classifier_model, device=-1)
 
48
 
 
49
  predictions = []
50
  for text in df['clean_content']:
51
  if not text.strip():
 
57
  df['class'] = predictions
58
  df = df.drop(columns=['clean_content'], errors='ignore')
59
 
 
60
  output_file = "output.csv"
61
  df.to_csv(output_file, index=False)
62
 
63
  return f"βœ… Success! Classified {len(df)} rows", output_file
 
64
  except Exception as e:
65
  return f"❌ Error: {str(e)}", None
66
 
67
+ # ====================== Q&A FUNCTION ======================
68
+ qa_pipeline = pipeline("question-answering", model=qa_model, device=-1)
69
+
70
+ def answer_question(news_content, question):
71
+ if not news_content or not question:
72
+ return "Please enter both news content and a question."
73
+ try:
74
+ result = qa_pipeline(question=question, context=news_content)
75
+ return f"**Answer:** {result['answer']}\n\n**Confidence:** {result['score']:.2%}"
76
+ except Exception as e:
77
+ return f"Error: {str(e)}"
78
+
79
  # ====================== GRADIO INTERFACE ======================
80
  with gr.Blocks(title="Daily Mirror News Classifier") as demo:
81
  gr.Markdown("# πŸ“° Daily Mirror News Classifier")
82
+ gr.Markdown("### Section 02 - Text Analytics Assignment")
83
+
84
+ with gr.Tabs():
85
+ # Tab 1: Classification
86
+ with gr.Tab("πŸ“Š Text Classification"):
87
+ gr.Markdown("Upload a CSV file with a `content` column")
88
+ file_input = gr.File(label="Upload CSV", file_types=[".csv"])
89
+ classify_btn = gr.Button("πŸš€ Classify News", variant="primary")
90
+ output_text = gr.Textbox(label="Status")
91
+ output_file = gr.File(label="Download output.csv")
92
+
93
+ classify_btn.click(
94
+ fn=classify_csv,
95
+ inputs=file_input,
96
+ outputs=[output_text, output_file]
97
+ )
98
+
99
+ # Tab 2: Q&A Pipeline
100
+ with gr.Tab("❓ Question Answering"):
101
+ gr.Markdown("Ask any question about a news article")
102
+ news_input = gr.Textbox(lines=10, label="Paste News Content", placeholder="Paste the full news article here...")
103
+ question_input = gr.Textbox(label="Your Question", placeholder="e.g., What is the main issue discussed?")
104
+ qa_btn = gr.Button("πŸ” Get Answer", variant="primary")
105
+ qa_output = gr.Textbox(label="Answer", lines=4)
106
+
107
+ qa_btn.click(
108
+ fn=answer_question,
109
+ inputs=[news_input, question_input],
110
+ outputs=qa_output
111
+ )
112
 
113
  gr.Markdown("Built for Text Analytics Assignment - Section 02")
114