shrirangphadke commited on
Commit
4066f8e
·
verified ·
1 Parent(s): de1b425

Update app.py

Browse files

Gradio sentiment signal

Files changed (1) hide show
  1. app.py +23 -18
app.py CHANGED
@@ -1,23 +1,28 @@
1
  import gradio as gr
2
- import tensorflow as tf
3
- import numpy as np
4
 
5
- # Load the pre-trained model
6
- model = tf.keras.applications.MobileNetV2()
7
- labels_path = tf.keras.utils.get_file(
8
- 'ImageNetLabels.txt', 'https://storage.googleapis.com/download.tensorflow.org/data/ImageNetLabels.txt')
9
- imagenet_labels = np.array(open(labels_path).read().splitlines())
 
 
 
 
 
 
 
 
10
 
11
- # Define the prediction function
12
- def classify_image(image):
13
- image = tf.keras.applications.mobilenet_v2.preprocess_input(image)
14
- predictions = model.predict(np.expand_dims(image, axis=0))
15
- return {imagenet_labels[i]: float(predictions[0][i]) for i in range(1000)}
16
 
17
- # Create a Gradio interface
18
- inputs = gr.inputs.Image(shape=(224, 224))
19
- outputs = gr.outputs.Label(num_top_classes=3)
20
- interface = gr.Interface(fn=classify_image, inputs=inputs, outputs=outputs, capture_session=True)
 
21
 
22
- # Launch the interface
23
- interface.launch()
 
1
  import gradio as gr
2
+ from textblob import TextBlob
 
3
 
4
+ def analyze_sentiment(text):
5
+ blob = TextBlob(text)
6
+ sentiment = blob.sentiment.polarity
7
+ if sentiment > 0:
8
+ color = "green"
9
+ label = "Positive"
10
+ elif sentiment < 0:
11
+ color = "red"
12
+ label = "Negative"
13
+ else:
14
+ color = "yellow"
15
+ label = "Neutral"
16
+ return color, label
17
 
18
+ def sentiment_analysis(text):
19
+ color, label = analyze_sentiment(text)
20
+ return "<h2 style='color:{}; text-align:center;'>{} Sentiment</h2>".format(color, label)
 
 
21
 
22
+ iface = gr.Interface(
23
+ fn=sentiment_analysis,
24
+ inputs=gr.inputs.Textbox(lines=5, label="Enter Text"),
25
+ outputs="html"
26
+ )
27
 
28
+ iface.launch()