riccardomusmeci commited on
Commit
24f62f4
·
verified ·
1 Parent(s): a4d60e0

Upload 2 files

Browse files
Files changed (2) hide show
  1. README.md +1 -1
  2. space.py +65 -0
README.md CHANGED
@@ -5,7 +5,7 @@ colorFrom: indigo
5
  colorTo: pink
6
  sdk: gradio
7
  sdk_version: "4.26.0"
8
- app_file: gradio.py
9
  pinned: false
10
  ---
11
 
 
5
  colorTo: pink
6
  sdk: gradio
7
  sdk_version: "4.26.0"
8
+ app_file: space.py
9
  pinned: false
10
  ---
11
 
space.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()