Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- README.md +17 -12
- gradio.py +65 -0
- requirements.txt +5 -0
README.md
CHANGED
|
@@ -1,12 +1,17 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SentimentProfAI - Gradio Space
|
| 2 |
+
|
| 3 |
+
Questo Space esegue sentiment analysis su recensioni di film usando TinyLlama + LoRA adapter.
|
| 4 |
+
|
| 5 |
+
## Come funziona
|
| 6 |
+
- Inserisci una recensione di un film
|
| 7 |
+
- Il modello risponde con label (positive/negative) e una breve motivazione
|
| 8 |
+
|
| 9 |
+
## Modello
|
| 10 |
+
- Base: TinyLlama/TinyLlama-1.1B-Chat-v1.0
|
| 11 |
+
- Adapter: riccardomusmeci/SentimentProfAI
|
| 12 |
+
|
| 13 |
+
## Dipendenze
|
| 14 |
+
Vedi `requirements.txt`.
|
| 15 |
+
|
| 16 |
+
## Autore
|
| 17 |
+
Riccardo Musmeci
|
gradio.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
from peft import PeftModel
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
MODEL_ID = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 8 |
+
ADAPTER_REPO = "riccardomusmeci/SentimentProfAI"
|
| 9 |
+
|
| 10 |
+
SYSTEM_PROMPT = """<|system|>
|
| 11 |
+
Analyze the sentiment of the following movie review and label it as positive or negative.
|
| 12 |
+
Provide ONLY an output in JSON format with two fields:
|
| 13 |
+
- "label": "positive" or "negative"
|
| 14 |
+
- "reasoning": a brief explanation of your classification
|
| 15 |
+
|
| 16 |
+
Do not add any other text after the JSON.</s>"""
|
| 17 |
+
|
| 18 |
+
device = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu"
|
| 19 |
+
|
| 20 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
+
MODEL_ID,
|
| 22 |
+
torch_dtype=torch.float16 if device in ["cuda", "mps"] else torch.float32,
|
| 23 |
+
)
|
| 24 |
+
tokenizer = AutoTokenizer.from_pretrained(ADAPTER_REPO)
|
| 25 |
+
model = PeftModel.from_pretrained(base_model, ADAPTER_REPO)
|
| 26 |
+
model.to(device)
|
| 27 |
+
model.eval()
|
| 28 |
+
|
| 29 |
+
def sentiment_analysis(review_text):
|
| 30 |
+
prompt = f"{SYSTEM_PROMPT}<|user|>\n{review_text}</s>\n<|assistant|>\n"
|
| 31 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 32 |
+
with torch.no_grad():
|
| 33 |
+
output = model.generate(
|
| 34 |
+
**inputs,
|
| 35 |
+
max_new_tokens=256,
|
| 36 |
+
do_sample=False,
|
| 37 |
+
pad_token_id=tokenizer.eos_token_id
|
| 38 |
+
)
|
| 39 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 40 |
+
# Estrai solo la parte JSON dalla risposta
|
| 41 |
+
try:
|
| 42 |
+
start = response.index('{')
|
| 43 |
+
end = response.rindex('}') + 1
|
| 44 |
+
json_str = response[start:end]
|
| 45 |
+
sentiment_json = json.loads(json_str)
|
| 46 |
+
label = sentiment_json.get("label", "")
|
| 47 |
+
reasoning = sentiment_json.get("reasoning", "")
|
| 48 |
+
except Exception:
|
| 49 |
+
label = "Errore"
|
| 50 |
+
reasoning = f"Impossibile estrarre il JSON. Output grezzo: {response}"
|
| 51 |
+
return label, reasoning
|
| 52 |
+
|
| 53 |
+
iface = gr.Interface(
|
| 54 |
+
fn=sentiment_analysis,
|
| 55 |
+
inputs=gr.Textbox(label="Movie Review"),
|
| 56 |
+
outputs=[
|
| 57 |
+
gr.Textbox(label="Label"),
|
| 58 |
+
gr.Textbox(label="Reasoning")
|
| 59 |
+
],
|
| 60 |
+
title="Sentiment Analysis ProfAI",
|
| 61 |
+
description="Analizza la recensione di un film e restituisce il sentiment (positivo/negativo) e la motivazione."
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers>=4.40.0
|
| 2 |
+
peft>=0.10.0
|
| 3 |
+
torch
|
| 4 |
+
accelerate
|
| 5 |
+
gradio
|