Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
from huggingface_hub import login
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# -----------------------------
|
| 8 |
+
# 1. Login using HF token (from environment variable)
|
| 9 |
+
# -----------------------------
|
| 10 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 11 |
+
if hf_token:
|
| 12 |
+
login(token=hf_token)
|
| 13 |
+
else:
|
| 14 |
+
print("⚠️ Warning: HF_TOKEN not set. Add it in the Space settings.")
|
| 15 |
+
|
| 16 |
+
# -----------------------------
|
| 17 |
+
# 2. Load Model
|
| 18 |
+
# -----------------------------
|
| 19 |
+
model_id = "ibm-granite/granite-3.3-2b-instruct"
|
| 20 |
+
|
| 21 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=True)
|
| 22 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 23 |
+
model_id,
|
| 24 |
+
torch_dtype=torch.float16,
|
| 25 |
+
device_map="auto",
|
| 26 |
+
use_auth_token=True
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# -----------------------------
|
| 30 |
+
# 3. Functions
|
| 31 |
+
# -----------------------------
|
| 32 |
+
def classify_sentiment(text):
|
| 33 |
+
|
| 34 |
+
prompt = f"""
|
| 35 |
+
Classify the sentiment of the following review as one word: Positive, Negative, or Neutral.
|
| 36 |
+
Review: "{text}"
|
| 37 |
+
Only respond with the sentiment label.
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 41 |
+
|
| 42 |
+
output = model.generate(
|
| 43 |
+
**inputs,
|
| 44 |
+
max_new_tokens=5,
|
| 45 |
+
temperature=0.1,
|
| 46 |
+
repetition_penalty=1.5
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
result = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 50 |
+
|
| 51 |
+
final = result.split()[-1].strip()
|
| 52 |
+
|
| 53 |
+
return final
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def summarize_text(text):
|
| 57 |
+
|
| 58 |
+
prompt = f"""
|
| 59 |
+
Summarize the following text in 3–5 bullet points.
|
| 60 |
+
Make the summary short, simple, and avoid repeating sentences.
|
| 61 |
+
Text: {text}
|
| 62 |
+
Summary:
|
| 63 |
+
"""
|
| 64 |
+
|
| 65 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 66 |
+
|
| 67 |
+
output = model.generate(
|
| 68 |
+
**inputs,
|
| 69 |
+
max_new_tokens=150,
|
| 70 |
+
temperature=0.7,
|
| 71 |
+
top_p=0.9,
|
| 72 |
+
repetition_penalty=2.0,
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
result = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 76 |
+
|
| 77 |
+
summary = result.split("Summary:")[-1].strip()
|
| 78 |
+
|
| 79 |
+
return summary
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
# -----------------------------
|
| 84 |
+
# 4. Gradio Interface
|
| 85 |
+
# -----------------------------
|
| 86 |
+
with gr.Blocks() as demo:
|
| 87 |
+
|
| 88 |
+
gr.Markdown("## 🟦 Granite 3.3-2B — Sentiment + Summarization Demo")
|
| 89 |
+
|
| 90 |
+
with gr.Tab("Sentiment Classification"):
|
| 91 |
+
inp = gr.Textbox(label="Enter Review", lines=4)
|
| 92 |
+
out = gr.Textbox(label="Predicted Sentiment", lines=1)
|
| 93 |
+
btn = gr.Button("Classify")
|
| 94 |
+
btn.click(classify_sentiment, inp, out)
|
| 95 |
+
|
| 96 |
+
with gr.Tab("Summarization"):
|
| 97 |
+
inp2 = gr.Textbox(label="Enter Text to Summarize", lines=8)
|
| 98 |
+
out2 = gr.Textbox(label="Summary", lines=8)
|
| 99 |
+
btn2 = gr.Button("Summarize")
|
| 100 |
+
btn2.click(summarize_text, inp2, out2)
|
| 101 |
+
|
| 102 |
+
demo.launch()
|