Ginidu2003 commited on
Commit
c7f80fa
·
verified ·
1 Parent(s): e310bda

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -0
app.py CHANGED
@@ -1,3 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # ====================== BEAUTIFUL UI ======================
2
  with gr.Blocks(
3
  title="English News Classifier",
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import torch
4
+ from transformers import pipeline
5
+ import nltk
6
+ from nltk.corpus import stopwords
7
+ from nltk.stem import WordNetLemmatizer
8
+ import re
9
+ import string
10
+ import matplotlib.pyplot as plt
11
+
12
+ # ====================== NLTK SETUP ======================
13
+ nltk.download('wordnet', quiet=True)
14
+ nltk.download('punkt', quiet=True)
15
+ nltk.download('punkt_tab', quiet=True)
16
+
17
+ lemmatizer = WordNetLemmatizer()
18
+
19
+ def preprocess_text(text):
20
+ if not isinstance(text, str):
21
+ return ""
22
+ text = text.lower()
23
+ punct_to_remove = string.punctuation.replace("'","").replace('"',"").replace("$","").replace("%","").replace("?","")
24
+ text = re.sub(f"[{punct_to_remove}]", " ", text)
25
+ tokens = nltk.word_tokenize(text)
26
+ tokens = [lemmatizer.lemmatize(word) for word in tokens]
27
+ return ' '.join(tokens)
28
+
29
+ classifier_model = "Ginidu2003/Distilbert-Base-News-classifier"
30
+
31
+ # ====================== BEAUTIFUL COLORED BAR CHART ======================
32
+ def create_colored_bar_chart(category_counts):
33
+ if category_counts is None or len(category_counts) == 0:
34
+ fig, ax = plt.subplots()
35
+ ax.text(0.5, 0.5, "No data available", ha='center', va='center')
36
+ return fig
37
+
38
+ categories = category_counts["Category"]
39
+ counts = category_counts["Count"]
40
+
41
+ # Nice modern color palette
42
+ colors = ['#3498DB', '#E67E22', '#9B59B6', '#2ECC71', '#E74C3C']
43
+
44
+ fig, ax = plt.subplots(figsize=(11, 6))
45
+ bars = ax.bar(categories, counts, color=colors, edgecolor='white', linewidth=0.8)
46
+
47
+ # Add value on top of bars
48
+ for bar in bars:
49
+ height = bar.get_height()
50
+ ax.text(bar.get_x() + bar.get_width()/2, height + 0.8,
51
+ str(int(height)), ha='center', va='bottom', fontsize=13, fontweight='bold')
52
+
53
+ ax.set_title("Category Distribution Across 5 Classes", fontsize=16, fontweight='bold', pad=20)
54
+ ax.set_xlabel("Category", fontsize=12)
55
+ ax.set_ylabel("Count", fontsize=12)
56
+ plt.xticks(rotation=15)
57
+ plt.tight_layout()
58
+ return fig
59
+
60
+ # ====================== CLASSIFICATION FUNCTION ======================
61
+ @torch.no_grad()
62
+ def classify_csv(file):
63
+ try:
64
+ df = pd.read_csv(file)
65
+ if 'content' not in df.columns:
66
+ return "Error: CSV must have a column named 'content'", None, None
67
+
68
+ df['clean_content'] = df['content'].apply(preprocess_text)
69
+
70
+ classifier = pipeline("text-classification", model=classifier_model, device=-1)
71
+
72
+ predictions = []
73
+ for text in df['clean_content']:
74
+ if not text.strip():
75
+ predictions.append("Unknown")
76
+ else:
77
+ result = classifier(text)[0]
78
+ predictions.append(result['label'])
79
+
80
+ df['class'] = predictions
81
+ df = df.drop(columns=['clean_content'], errors='ignore')
82
+
83
+ output_file = "output.csv"
84
+ df.to_csv(output_file, index=False)
85
+
86
+ category_counts = df['class'].value_counts().reset_index()
87
+ category_counts.columns = ["Category", "Count"]
88
+
89
+ fig = create_colored_bar_chart(category_counts)
90
+
91
+ return f"✅ Success! Classified {len(df)} rows", output_file, fig
92
+ except Exception as e:
93
+ return f"❌ Error: {str(e)}", None, None
94
+
95
+ # ====================== Q&A FUNCTION ======================
96
+ from transformers import AutoTokenizer, AutoModelForQuestionAnswering
97
+ qa_tokenizer = AutoTokenizer.from_pretrained("deepset/roberta-base-squad2")
98
+ qa_model = AutoModelForQuestionAnswering.from_pretrained("deepset/roberta-base-squad2")
99
+
100
+ def answer_question(news_content, question):
101
+ if not news_content.strip() or not question.strip():
102
+ return "Please enter both news content and a question."
103
+ try:
104
+ inputs = qa_tokenizer(question, news_content, return_tensors="pt", truncation=True, max_length=512)
105
+ with torch.no_grad():
106
+ outputs = qa_model(**inputs)
107
+
108
+ start_idx = torch.argmax(outputs.start_logits)
109
+ end_idx = torch.argmax(outputs.end_logits) + 1
110
+
111
+ answer = qa_tokenizer.decode(inputs.input_ids[0][start_idx:end_idx],
112
+ skip_special_tokens=True,
113
+ clean_up_tokenization_spaces=True)
114
+
115
+ confidence = torch.max(torch.softmax(outputs.start_logits, dim=1)).item()
116
+
117
+ return f"**Answer:** {answer.strip()}\n\n**Confidence:** {confidence:.2%}"
118
+ except Exception as e:
119
+ return f"Error: {str(e)}"
120
+
121
  # ====================== BEAUTIFUL UI ======================
122
  with gr.Blocks(
123
  title="English News Classifier",