Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,125 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 3 |
import torch
|
|
|
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
# Load Model and Tokenizer
|
| 11 |
try:
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
model
|
| 15 |
-
|
| 16 |
-
print(f"Model loaded successfully from Hugging Face Hub: {model_hub_path}")
|
| 17 |
except Exception as e:
|
| 18 |
-
print(f"
|
| 19 |
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
if not text or len(text.strip()) < 5:
|
| 25 |
-
return "Please enter a longer news snippet or headline.", {}
|
| 26 |
-
|
| 27 |
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
| 28 |
-
|
| 29 |
with torch.no_grad():
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
examples=[
|
| 58 |
-
["The global stock market rallied today after the central bank cut interest rates."],
|
| 59 |
-
["New research shows quantum entanglement may enable faster computing."],
|
| 60 |
-
["Manchester United defeats Liverpool in a stunning Premier League match."],
|
| 61 |
-
["The President's cabinet held an emergency summit on trade negotiations."],
|
| 62 |
-
["AT&T Wireless ships mobile IM gadget US mobile network operator AT&T Wireless today launched Ogo, its first non-voice messaging device, pitche..."]
|
| 63 |
-
]
|
| 64 |
-
)
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
| 67 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
+
import pandas as pd
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# --- Configuration ---
|
| 8 |
+
# Use your verified public model path
|
| 9 |
+
MODEL_HUB_PATH = "AJC1/ag_news_distilbert_finetuned"
|
| 10 |
+
TARGET_NAMES = ["World", "Sports", "Business", "Sci/Tech"]
|
| 11 |
|
| 12 |
+
# --- 1. Load Model (Cached for performance) ---
|
|
|
|
| 13 |
try:
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_HUB_PATH)
|
| 15 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_HUB_PATH)
|
| 16 |
+
model.eval()
|
| 17 |
+
print("Model loaded successfully.")
|
|
|
|
| 18 |
except Exception as e:
|
| 19 |
+
print(f"Error loading model: {e}")
|
| 20 |
|
| 21 |
+
# --- 2. Prediction Functions ---
|
| 22 |
|
| 23 |
+
def predict_single_text(text):
|
| 24 |
+
"""The core logic: text -> dict of scores"""
|
| 25 |
+
if not text: return {}
|
|
|
|
|
|
|
|
|
|
| 26 |
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
|
|
|
| 27 |
with torch.no_grad():
|
| 28 |
+
logits = model(**inputs).logits
|
| 29 |
+
probs = torch.softmax(logits, dim=1)[0].tolist()
|
| 30 |
+
return {TARGET_NAMES[i]: v for i, v in enumerate(probs)}
|
| 31 |
+
|
| 32 |
+
def process_csv_file(file_obj):
|
| 33 |
+
"""The enterprise logic: CSV -> Classified CSV"""
|
| 34 |
+
try:
|
| 35 |
+
# Load the uploaded CSV
|
| 36 |
+
df = pd.read_csv(file_obj.name)
|
| 37 |
|
| 38 |
+
# Validation: Check if it has text
|
| 39 |
+
if df.empty:
|
| 40 |
+
return None, "Error: Uploaded file is empty."
|
| 41 |
+
|
| 42 |
+
# Smart column detection: Look for 'text', 'headline', or use the first column
|
| 43 |
+
target_col = None
|
| 44 |
+
for col in ['text', 'headline', 'title', 'content']:
|
| 45 |
+
if col in df.columns:
|
| 46 |
+
target_col = col
|
| 47 |
+
break
|
| 48 |
+
if not target_col:
|
| 49 |
+
target_col = df.columns[0] # Fallback to first column
|
| 50 |
+
|
| 51 |
+
# Run predictions (Iterating for safety, batching could be faster but more complex)
|
| 52 |
+
predicted_labels = []
|
| 53 |
+
confidence_scores = []
|
| 54 |
+
|
| 55 |
+
for text in df[target_col].astype(str):
|
| 56 |
+
scores = predict_single_text(text)
|
| 57 |
+
# Get the top label
|
| 58 |
+
top_label = max(scores, key=scores.get)
|
| 59 |
+
predicted_labels.append(top_label)
|
| 60 |
+
confidence_scores.append(f"{scores[top_label]:.2f}")
|
| 61 |
+
|
| 62 |
+
# Add results to dataframe
|
| 63 |
+
df['Predicted_Category'] = predicted_labels
|
| 64 |
+
df['Confidence'] = confidence_scores
|
| 65 |
+
|
| 66 |
+
# Save to a temporary output file
|
| 67 |
+
output_path = "classified_results.csv"
|
| 68 |
+
df.to_csv(output_path, index=False)
|
| 69 |
+
|
| 70 |
+
return output_path, f"Success! Processed {len(df)} rows. Download your results below."
|
| 71 |
+
|
| 72 |
+
except Exception as e:
|
| 73 |
+
return None, f"Error processing file: {str(e)}"
|
| 74 |
+
|
| 75 |
+
# --- 3. The Professional Tabbed Interface ---
|
| 76 |
+
with gr.Blocks(title="AG News Enterprise Classifier") as demo:
|
| 77 |
|
| 78 |
+
gr.Markdown("# 📰 Automated News Routing System")
|
| 79 |
+
gr.Markdown("Select a workflow below: Single-item checking or Bulk file processing.")
|
| 80 |
+
|
| 81 |
+
with gr.Tabs():
|
| 82 |
+
|
| 83 |
+
# === TAB 1: Single Input (For Demo/Editors) ===
|
| 84 |
+
with gr.TabItem("Live Check"):
|
| 85 |
+
with gr.Row():
|
| 86 |
+
with gr.Column():
|
| 87 |
+
text_input = gr.Textbox(lines=4, label="Input News Headline", placeholder="Paste text here...")
|
| 88 |
+
submit_btn = gr.Button("Classify Content", variant="primary")
|
| 89 |
+
|
| 90 |
+
with gr.Column():
|
| 91 |
+
label_output = gr.Label(num_top_classes=4, label="Category Prediction")
|
| 92 |
+
|
| 93 |
+
# Link functionality
|
| 94 |
+
submit_btn.click(fn=predict_single_text, inputs=text_input, outputs=label_output)
|
| 95 |
+
|
| 96 |
+
# Examples
|
| 97 |
+
gr.Examples(
|
| 98 |
+
examples=[
|
| 99 |
+
["Wall Street tumbles as tech stocks sell off."],
|
| 100 |
+
["Manchester United signs new striker for record fee."],
|
| 101 |
+
["NASA discovers water on Mars surface."]
|
| 102 |
+
],
|
| 103 |
+
inputs=text_input
|
| 104 |
+
)
|
| 105 |
|
| 106 |
+
# === TAB 2: Batch Processing (For Operations) ===
|
| 107 |
+
with gr.TabItem("Bulk Analysis (CSV)"):
|
| 108 |
+
gr.Markdown("Upload a CSV file containing news headlines. The system will append a 'Category' column and return the file.")
|
| 109 |
+
|
| 110 |
+
with gr.Row():
|
| 111 |
+
file_input = gr.File(label="Upload CSV File", file_types=[".csv"])
|
| 112 |
+
file_output = gr.File(label="Download Classified Results")
|
| 113 |
+
|
| 114 |
+
status_text = gr.Textbox(label="Status", interactive=False)
|
| 115 |
+
process_btn = gr.Button("Process Batch", variant="primary")
|
| 116 |
+
|
| 117 |
+
# Link functionality
|
| 118 |
+
process_btn.click(
|
| 119 |
+
fn=process_csv_file,
|
| 120 |
+
inputs=file_input,
|
| 121 |
+
outputs=[file_output, status_text]
|
| 122 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
if __name__ == "__main__":
|
| 125 |
+
demo.launch()
|