Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,156 +1,23 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
-
from datetime import datetime
|
4 |
-
import json
|
5 |
-
import numpy as np
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
sentiment_analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
10 |
-
text_generator = pipeline("text-generation", model="gpt2")
|
11 |
-
except Exception as e:
|
12 |
-
print(f"Model loading error: {e}")
|
13 |
-
raise
|
14 |
-
|
15 |
-
# Custom CSS for better UI
|
16 |
-
custom_css = """
|
17 |
-
footer {visibility: hidden}
|
18 |
-
.important-text {
|
19 |
-
font-size: 14px;
|
20 |
-
color: #666;
|
21 |
-
font-style: italic;
|
22 |
-
}
|
23 |
-
"""
|
24 |
|
25 |
def analyze_sentiment(text):
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
"analysis": {
|
31 |
-
"sentiment": result["label"],
|
32 |
-
"confidence": float(result["score"]),
|
33 |
-
"processing_time": str(datetime.now() - start_time)
|
34 |
-
},
|
35 |
-
"original_text": text
|
36 |
-
}
|
37 |
-
except Exception as e:
|
38 |
-
return {"error": str(e)}
|
39 |
-
|
40 |
-
def generate_text(prompt, length=50, temperature=0.7):
|
41 |
-
start_time = datetime.now()
|
42 |
-
try:
|
43 |
-
generated = text_generator(
|
44 |
-
prompt,
|
45 |
-
max_length=length,
|
46 |
-
num_return_sequences=1,
|
47 |
-
temperature=temperature
|
48 |
-
)
|
49 |
-
return {
|
50 |
-
"generated_text": generated[0]["generated_text"],
|
51 |
-
"metadata": {
|
52 |
-
"model": "GPT-2",
|
53 |
-
"length": length,
|
54 |
-
"temperature": temperature,
|
55 |
-
"processing_time": str(datetime.now() - start_time)
|
56 |
-
}
|
57 |
-
}
|
58 |
-
except Exception as e:
|
59 |
-
return {"error": str(e)}
|
60 |
|
61 |
-
with gr.Blocks(
|
62 |
-
|
63 |
-
|
64 |
-
css=custom_css
|
65 |
-
) as demo:
|
66 |
-
gr.Markdown("""
|
67 |
-
# 🏭 NLP Production Endpoint
|
68 |
-
**Enterprise-ready NLP services** with monitoring capabilities
|
69 |
-
""")
|
70 |
-
|
71 |
-
with gr.Tab("Sentiment Analysis"):
|
72 |
-
with gr.Row():
|
73 |
-
with gr.Column(scale=2):
|
74 |
-
sentiment_input = gr.Textbox(
|
75 |
-
label="Input Text",
|
76 |
-
placeholder="Enter text to analyze...",
|
77 |
-
lines=3
|
78 |
-
)
|
79 |
-
with gr.Accordion("Advanced Options", open=False):
|
80 |
-
gr.Markdown("No additional options for sentiment analysis", elem_classes="important-text")
|
81 |
-
sentiment_button = gr.Button("Analyze", variant="primary")
|
82 |
-
|
83 |
-
with gr.Column(scale=3):
|
84 |
-
sentiment_output = gr.JSON(
|
85 |
-
label="Analysis Results",
|
86 |
-
container=True
|
87 |
-
)
|
88 |
-
|
89 |
-
gr.Examples(
|
90 |
-
examples=[
|
91 |
-
"This product revolutionized our workflow!",
|
92 |
-
"The service was unsatisfactory and slow.",
|
93 |
-
"It meets basic requirements but lacks innovation."
|
94 |
-
],
|
95 |
-
inputs=sentiment_input,
|
96 |
-
label="Try these examples"
|
97 |
-
)
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
gen_input = gr.Textbox(
|
103 |
-
label="Prompt",
|
104 |
-
placeholder="Start your creative writing here...",
|
105 |
-
lines=3
|
106 |
-
)
|
107 |
-
with gr.Accordion("Generation Parameters", open=False):
|
108 |
-
gen_length = gr.Slider(20, 200, value=50, label="Output Length")
|
109 |
-
gen_temp = gr.Slider(0.1, 1.0, value=0.7, step=0.1, label="Creativity (Temperature)")
|
110 |
-
gen_button = gr.Button("Generate Text", variant="primary")
|
111 |
-
|
112 |
-
with gr.Column(scale=3):
|
113 |
-
gen_output = gr.JSON(
|
114 |
-
label="Generated Output",
|
115 |
-
container=True
|
116 |
-
)
|
117 |
-
|
118 |
-
# Monitoring section (hidden by default)
|
119 |
-
with gr.Accordion("API Monitoring", open=False):
|
120 |
-
gr.Markdown("""
|
121 |
-
### Performance Metrics
|
122 |
-
- Last request time: `2025-04-29 15:36:26`
|
123 |
-
- Average processing time: `0.45s`
|
124 |
-
- System health: ✅ Operational
|
125 |
-
""")
|
126 |
-
|
127 |
-
# Footer
|
128 |
-
gr.Markdown("---")
|
129 |
-
gr.HTML("""
|
130 |
-
<div style="text-align: center">
|
131 |
-
<p>Powered by Hugging Face Transformers | Gradio {version} | Python 3.10</p>
|
132 |
-
<p>Build SHA: 6615a41 | Queued at 2025-04-29 15:36:26</p>
|
133 |
-
</div>
|
134 |
-
""".format(version=gr.__version__))
|
135 |
|
136 |
-
|
137 |
-
sentiment_button.click(
|
138 |
-
fn=analyze_sentiment,
|
139 |
-
inputs=sentiment_input,
|
140 |
-
outputs=sentiment_output,
|
141 |
-
api_name="analyze_sentiment"
|
142 |
-
)
|
143 |
-
|
144 |
-
gen_button.click(
|
145 |
-
fn=generate_text,
|
146 |
-
inputs=[gen_input, gen_length, gen_temp],
|
147 |
-
outputs=gen_output,
|
148 |
-
api_name="generate_text"
|
149 |
-
)
|
150 |
|
151 |
-
|
152 |
-
demo.launch(
|
153 |
-
server_name="0.0.0.0",
|
154 |
-
server_port=7860,
|
155 |
-
show_api=True
|
156 |
-
)
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
|
|
|
|
|
|
3 |
|
4 |
+
# Load sentiment-analysis pipeline
|
5 |
+
classifier = pipeline("sentiment-analysis")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
def analyze_sentiment(text):
|
8 |
+
result = classifier(text)[0]
|
9 |
+
label = result['label']
|
10 |
+
score = result['score']
|
11 |
+
return f"Sentiment: {label} ({score:.2f})"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
with gr.Blocks() as demo:
|
14 |
+
gr.Markdown("# 🧠 Sentiment Analyzer")
|
15 |
+
gr.Markdown("Enter text and get the sentiment prediction!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
text_input = gr.Textbox(label="Enter your text")
|
18 |
+
analyze_button = gr.Button("Analyze Sentiment")
|
19 |
+
output = gr.Textbox(label="Result")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
analyze_button.click(fn=analyze_sentiment, inputs=text_input, outputs=output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|