Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load a Hugging Face model for rewriting
|
| 5 |
+
rewrite_pipeline = pipeline("text2text-generation", model="t5-small")
|
| 6 |
+
classification_pipeline = pipeline("text-classification", model="textattack/bert-base-uncased-imdb")
|
| 7 |
+
|
| 8 |
+
def rewrite_and_analyze(input_text, temperature):
|
| 9 |
+
# Generate rewritten text
|
| 10 |
+
rewritten = rewrite_pipeline(input_text, temperature=temperature, max_length=200)
|
| 11 |
+
rewritten_text = rewritten[0]['generated_text']
|
| 12 |
+
|
| 13 |
+
# Analyze classification probabilities
|
| 14 |
+
analysis = classification_pipeline(rewritten_text, return_all_scores=True)[0]
|
| 15 |
+
class_probs = {item['label']: round(item['score'], 3) for item in analysis}
|
| 16 |
+
|
| 17 |
+
return rewritten_text, class_probs
|
| 18 |
+
|
| 19 |
+
# Gradio interface
|
| 20 |
+
inputs = [
|
| 21 |
+
gr.Textbox(lines=5, placeholder="Enter text here...", label="Input Text"),
|
| 22 |
+
gr.Slider(0.1, 2.0, value=1.0, label="Temperature")
|
| 23 |
+
]
|
| 24 |
+
outputs = [
|
| 25 |
+
gr.Textbox(label="Rewritten Text"),
|
| 26 |
+
gr.JSON(label="Class Probabilities")
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
app = gr.Interface(
|
| 30 |
+
fn=rewrite_and_analyze,
|
| 31 |
+
inputs=inputs,
|
| 32 |
+
outputs=outputs,
|
| 33 |
+
title="Text Rewriter and Classifier"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
app.launch()
|