Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
# Load your model
|
| 6 |
+
print("Loading model...")
|
| 7 |
+
classifier = pipeline(
|
| 8 |
+
"text-classification",
|
| 9 |
+
model="LokeshDevCreates/tone-baseline-v3",
|
| 10 |
+
top_k=None # Return all labels with scores
|
| 11 |
+
)
|
| 12 |
+
print("Model loaded successfully!")
|
| 13 |
+
|
| 14 |
+
def classify_tone(text):
|
| 15 |
+
"""Classify tone of input text"""
|
| 16 |
+
try:
|
| 17 |
+
results = classifier(text)[0]
|
| 18 |
+
# Sort by score descending
|
| 19 |
+
results = sorted(results, key=lambda x: x['score'], reverse=True)
|
| 20 |
+
|
| 21 |
+
# Return as dict for easy JSON parsing
|
| 22 |
+
return {
|
| 23 |
+
"detected_tone": results[0]['label'],
|
| 24 |
+
"confidence": round(results[0]['score'], 4),
|
| 25 |
+
"all_probs": {r['label']: round(r['score'], 4) for r in results}
|
| 26 |
+
}
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return {"error": str(e)}
|
| 29 |
+
|
| 30 |
+
# Create Gradio interface
|
| 31 |
+
demo = gr.Interface(
|
| 32 |
+
fn=classify_tone,
|
| 33 |
+
inputs=gr.Textbox(
|
| 34 |
+
label="Text to Analyze",
|
| 35 |
+
placeholder="Enter text here...",
|
| 36 |
+
lines=3
|
| 37 |
+
),
|
| 38 |
+
outputs=gr.JSON(label="Tone Analysis"),
|
| 39 |
+
title="Tone Detection API",
|
| 40 |
+
description="Detect the tone of text using tone-baseline-v3 model",
|
| 41 |
+
examples=[
|
| 42 |
+
["This is absolutely terrible and I hate it!"],
|
| 43 |
+
["Thank you so much for your help!"],
|
| 44 |
+
["The meeting is scheduled for 3pm tomorrow."],
|
| 45 |
+
]
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
demo.launch()
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
### **Step 3: Create `requirements.txt`**
|
| 53 |
+
|
| 54 |
+
Add another file `requirements.txt`:
|
| 55 |
+
```
|
| 56 |
+
transformers
|
| 57 |
+
torch
|
| 58 |
+
gradio
|