File size: 2,002 Bytes
11f6c89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
58
59
60
61
62
import os
import gradio as gr
import transformers
from pyabsa import AspectTermExtraction as ATEPC
import warnings

# 1. Compatibility setup for Hugging Face Spaces
warnings.filterwarnings("ignore")
transformers.PretrainedConfig.is_decoder = False
transformers.PretrainedConfig.output_attentions = False
transformers.PretrainedConfig.output_hidden_states = False

# 2. Path to the model
# When uploaded to HF, the 'model' folder should be in the root
CHECKPOINT_PATH = "model"

print(f"Loading model from: {CHECKPOINT_PATH}...")
# Load the model once at startup
model = ATEPC.AspectExtractor(checkpoint=CHECKPOINT_PATH)

def predict_absa(text):
    if not text.strip():
        return "Please enter some text to analyze."
    
    # Run prediction
    result = model.predict(text, print_result=False)
    
    if not result['aspect']:
        return "No aspects found in the input text."
    
    # Format results for display
    output = []
    for aspect, sentiment in zip(result['aspect'], result['sentiment']):
        output.append({
            "Aspect": aspect,
            "Sentiment": sentiment
        })
    
    return output

# 3. Create Gradio Interface
demo = gr.Interface(
    fn=predict_absa,
    inputs=gr.Textbox(
        lines=3, 
        placeholder="Enter a sentence here (e.g., 'The coffee was great but the price was too high.')",
        label="Input Text"
    ),
    outputs=gr.JSON(label="ABSA Results"),
    title="DeBERTa-v3 Aspect Based Sentiment Analysis",
    description="This demo uses a fine-tuned DeBERTa-v3 model to extract aspects and classify their sentiment polarities.",
    examples=[
        ["The food was delicious but the service was extremely slow."],
        ["The battery life of this laptop is amazing, though the screen is a bit dim."],
        ["I love the interface, but the mobile app crashes frequently."]
    ],
    cache_examples=False
)

if __name__ == "__main__":
    demo.launch()