AJC1 commited on
Commit
d9b7cea
·
verified ·
1 Parent(s): 745cc84

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +81 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
3
+ import torch
4
+ import os
5
+
6
+ # --- Configuration ---
7
+ MODEL_PATH = "ag_news_distilbert_finetuned" # This is the folder saved in your ZIP file
8
+ TARGET_NAMES = ["World", "Sports", "Business", "Sci/Tech"]
9
+
10
+ # Check if the model files are present (critical for local/Spaces deployment)
11
+ if not os.path.exists(MODEL_PATH):
12
+ print(f"Error: Model directory '{MODEL_PATH}' not found. Ensure you have extracted the ZIP file.")
13
+ # Exit or raise error if running locally
14
+ # If running on HF Spaces, the folder structure must match.
15
+
16
+ # --- 1. Load Model and Tokenizer (The Persistence Step) ---
17
+ try:
18
+ # Use the local model path to load the fine-tuned weights
19
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
20
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
21
+ model.eval() # Set the model to evaluation mode
22
+ print("Model and Tokenizer loaded successfully from local directory.")
23
+ except Exception as e:
24
+ print(f"Failed to load model from path: {e}")
25
+ # Fallback or exit logic if needed
26
+
27
+
28
+ # --- 2. Define the Prediction Function ---
29
+ def classify_news(text):
30
+ """Takes input text, tokenizes it, and returns the predicted class name and confidence scores."""
31
+ if not text or len(text.strip()) < 5:
32
+ return "Please enter a longer news snippet or headline.", {}
33
+
34
+ # a. Tokenize and prepare input
35
+ inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
36
+
37
+ # b. Run inference (crucial to use torch.no_grad() for efficient inference)
38
+ with torch.no_grad():
39
+ outputs = model(**inputs)
40
+ logits = outputs.logits
41
+ probabilities = torch.softmax(logits, dim=1)[0].tolist()
42
+
43
+ # c. Map probabilities to labels
44
+ confidence = {
45
+ TARGET_NAMES[i]: prob for i, prob in enumerate(probabilities)
46
+ }
47
+
48
+ # d. Return the results
49
+ return confidence
50
+
51
+ # --- 3. Create the Gradio Interface ---
52
+ iface = gr.Interface(
53
+ fn=classify_news,
54
+ inputs=gr.Textbox(
55
+ lines=5,
56
+ label="Input News Headline or Snippet",
57
+ placeholder="Example: Russia and Canada discuss gas pipeline auction..."
58
+ ),
59
+ outputs=gr.Label(
60
+ num_top_classes=4, # Display all 4 classes and their scores
61
+ label="Predicted Category and Confidence"
62
+ ),
63
+ title="AG News Classifier: Fine-Tuned DistilBERT",
64
+ description=(
65
+ "Enter a news headline or short article. The model will predict its category "
66
+ "(World, Sports, Business, or Sci/Tech) and show the confidence scores for all categories."
67
+ ),
68
+ # Add examples from your analysis to show it handles tricky inputs
69
+ examples=[
70
+ ["The global stock market rallied today after the central bank cut interest rates."], # Business
71
+ ["New research shows quantum entanglement may enable faster computing."], # Sci/Tech
72
+ ["Manchester United defeats Liverpool in a stunning Premier League match."], # Sports
73
+ ["The President's cabinet held an emergency summit on trade negotiations."], # World
74
+ ["AT&T Wireless ships mobile IM gadget US mobile network operator AT&T Wireless today launched Ogo, its first non-voice messaging device, pitche..."] # Misclassified Example
75
+ ],
76
+ allow_flagging="never", # Optional: prevents user from submitting feedback
77
+ )
78
+
79
+ # Launch the application
80
+ if __name__ == "__main__":
81
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ transformers
2
+ torch
3
+ gradio