File size: 1,466 Bytes
26a7d3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b90d4a
26a7d3a
 
 
 
 
 
 
 
 
 
 
 
 
 
a7b8697
3b90d4a
26a7d3a
 
 
3b90d4a
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
import gradio as gr
from transformers import pipeline
import json

# Load your model
print("Loading model...")
classifier = pipeline(
    "text-classification",
    model="LokeshDevCreates/tone-baseline-v3",
    top_k=None  # Return all labels with scores
)
print("Model loaded successfully!")

def classify_tone(text):
    """Classify tone of input text"""
    try:
        results = classifier(text)[0]
        # Sort by score descending
        results = sorted(results, key=lambda x: x['score'], reverse=True)
        
        # Return as dict for easy JSON parsing
        return {
            "detected_tone": results[0]['label'],
            "confidence": round(results[0]['score'], 4),
            "all_probs": {r['label']: round(r['score'], 4) for r in results}
        }
    except Exception as e:
        return {"error": str(e)}

# Create Gradio interface
demo = gr.Interface(
    fn=classify_tone,
    inputs=gr.Textbox(
        label="Text to Analyze",
        placeholder="Enter text here...",
        lines=3
    ),
    outputs=gr.JSON(label="Tone Analysis"),
    title="Tone Detection API",
    description="Detect the tone of text using tone-baseline-v3 model",
    examples=[
        ["This is absolutely terrible and I hate it!"],
        ["Thank you so much for your help!"],
        ["The meeting is scheduled for 3pm tomorrow."],
    ],
    api_name="predict"  # Explicitly name the API endpoint
)

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