Spaces:
Configuration error
Configuration error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import dash
|
2 |
+
from dash import dcc, html, Input, Output, State
|
3 |
+
import dash_bootstrap_components as dbc
|
4 |
+
from transformers import pipeline
|
5 |
+
import plotly.graph_objects as go
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Initialize Hugging Face pipelines
|
9 |
+
try:
|
10 |
+
sentiment_pipeline = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment-latest")
|
11 |
+
text_generator = pipeline("text-generation", model="gpt2", max_length=100)
|
12 |
+
except Exception as e:
|
13 |
+
print(f"Error loading models: {e}")
|
14 |
+
sentiment_pipeline = None
|
15 |
+
text_generator = None
|
16 |
+
|
17 |
+
# Initialize Dash app
|
18 |
+
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
19 |
+
app.title = "Hugging Face Dash Demo"
|
20 |
+
|
21 |
+
# Define the layout
|
22 |
+
app.layout = dbc.Container([
|
23 |
+
dbc.Row([
|
24 |
+
dbc.Col([
|
25 |
+
html.H1("🤗 Hugging Face + Dash Demo", className="text-center mb-4"),
|
26 |
+
html.Hr(),
|
27 |
+
])
|
28 |
+
]),
|
29 |
+
|
30 |
+
dbc.Row([
|
31 |
+
dbc.Col([
|
32 |
+
dbc.Card([
|
33 |
+
dbc.CardBody([
|
34 |
+
html.H4("Sentiment Analysis", className="card-title"),
|
35 |
+
dcc.Textarea(
|
36 |
+
id='sentiment-input',
|
37 |
+
placeholder='Enter text to analyze sentiment...',
|
38 |
+
style={'width': '100%', 'height': 100},
|
39 |
+
className="mb-3"
|
40 |
+
),
|
41 |
+
dbc.Button("Analyze Sentiment", id="sentiment-btn", color="primary", className="mb-3"),
|
42 |
+
html.Div(id='sentiment-output')
|
43 |
+
])
|
44 |
+
])
|
45 |
+
], width=6),
|
46 |
+
|
47 |
+
dbc.Col([
|
48 |
+
dbc.Card([
|
49 |
+
dbc.CardBody([
|
50 |
+
html.H4("Text Generation", className="card-title"),
|
51 |
+
dcc.Textarea(
|
52 |
+
id='generation-input',
|
53 |
+
placeholder='Enter prompt for text generation...',
|
54 |
+
style={'width': '100%', 'height': 100},
|
55 |
+
className="mb-3"
|
56 |
+
),
|
57 |
+
dbc.Button("Generate Text", id="generation-btn", color="success", className="mb-3"),
|
58 |
+
html.Div(id='generation-output')
|
59 |
+
])
|
60 |
+
])
|
61 |
+
], width=6)
|
62 |
+
], className="mb-4"),
|
63 |
+
|
64 |
+
dbc.Row([
|
65 |
+
dbc.Col([
|
66 |
+
dbc.Card([
|
67 |
+
dbc.CardBody([
|
68 |
+
html.H4("Sentiment Score Visualization", className="card-title"),
|
69 |
+
dcc.Graph(id='sentiment-graph')
|
70 |
+
])
|
71 |
+
])
|
72 |
+
])
|
73 |
+
])
|
74 |
+
], fluid=True)
|
75 |
+
|
76 |
+
# Callback for sentiment analysis
|
77 |
+
@app.callback(
|
78 |
+
[Output('sentiment-output', 'children'),
|
79 |
+
Output('sentiment-graph', 'figure')],
|
80 |
+
[Input('sentiment-btn', 'n_clicks')],
|
81 |
+
[State('sentiment-input', 'value')]
|
82 |
+
)
|
83 |
+
def analyze_sentiment(n_clicks, text):
|
84 |
+
if not n_clicks or not text or not sentiment_pipeline:
|
85 |
+
return "Enter text and click 'Analyze Sentiment'", {}
|
86 |
+
|
87 |
+
try:
|
88 |
+
result = sentiment_pipeline(text)
|
89 |
+
label = result[0]['label']
|
90 |
+
score = result[0]['score']
|
91 |
+
|
92 |
+
# Create output
|
93 |
+
output = dbc.Alert([
|
94 |
+
html.H5(f"Sentiment: {label}"),
|
95 |
+
html.P(f"Confidence: {score:.2%}")
|
96 |
+
], color="info")
|
97 |
+
|
98 |
+
# Create visualization
|
99 |
+
colors = {'POSITIVE': 'green', 'NEGATIVE': 'red', 'NEUTRAL': 'orange'}
|
100 |
+
fig = go.Figure(data=[
|
101 |
+
go.Bar(x=[label], y=[score], marker_color=colors.get(label, 'blue'))
|
102 |
+
])
|
103 |
+
fig.update_layout(
|
104 |
+
title="Sentiment Analysis Result",
|
105 |
+
xaxis_title="Sentiment",
|
106 |
+
yaxis_title="Confidence Score",
|
107 |
+
yaxis=dict(range=[0, 1])
|
108 |
+
)
|
109 |
+
|
110 |
+
return output, fig
|
111 |
+
|
112 |
+
except Exception as e:
|
113 |
+
return dbc.Alert(f"Error: {str(e)}", color="danger"), {}
|
114 |
+
|
115 |
+
# Callback for text generation
|
116 |
+
@app.callback(
|
117 |
+
Output('generation-output', 'children'),
|
118 |
+
[Input('generation-btn', 'n_clicks')],
|
119 |
+
[State('generation-input', 'value')]
|
120 |
+
)
|
121 |
+
def generate_text(n_clicks, prompt):
|
122 |
+
if not n_clicks or not prompt or not text_generator:
|
123 |
+
return "Enter a prompt and click 'Generate Text'"
|
124 |
+
|
125 |
+
try:
|
126 |
+
result = text_generator(prompt, max_length=len(prompt.split()) + 50, num_return_sequences=1)
|
127 |
+
generated_text = result[0]['generated_text']
|
128 |
+
|
129 |
+
return dbc.Alert([
|
130 |
+
html.H5("Generated Text:"),
|
131 |
+
html.P(generated_text)
|
132 |
+
], color="success")
|
133 |
+
|
134 |
+
except Exception as e:
|
135 |
+
return dbc.Alert(f"Error: {str(e)}", color="danger")
|
136 |
+
|
137 |
+
# Run the app
|
138 |
+
if __name__ == '__main__':
|
139 |
+
# Hugging Face Spaces requires the app to run on port 7860
|
140 |
+
app.run_server(host='0.0.0.0', port=7860, debug=False)
|