suryabbrj commited on
Commit
9d5c1d8
·
1 Parent(s): f0a9d5d

fixed some parsing issues, optimized payload delivery to reduce prediction time

Browse files

this commit includes, some bug fixes and text formatting, to the previous version.

Files changed (1) hide show
  1. app.py +16 -12
app.py CHANGED
@@ -1,25 +1,30 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
-
4
  import warnings
5
  import logging
6
  warnings.simplefilter('ignore')
7
  logging.disable(logging.WARNING)
8
 
9
- model_path = "cardiffnlp/twitter-xlm-roberta-base-sentiment"
10
 
11
 
12
  def predict(image):
13
  cap = pipeline('image-to-text')
14
- genereated_dict = cap(image)
 
15
 
16
- final = str(genereated_dict)
17
- def sentiment_analysis(phrase):
18
  pipe = pipeline('text-classification')
19
- sentiment = pipe(phrase)
20
- return str(senti)
21
- senti = sentiment_analysis(final)
22
- output = final[20:-2]
 
 
 
 
 
 
23
  return output
24
 
25
 
@@ -27,13 +32,12 @@ input = gr.inputs.Image(
27
  label="Upload your Image and wait for 8-12 seconds!", type='pil', optional=False)
28
  output = gr.outputs.Textbox(label="Captions")
29
 
30
- title = "Content ModX UI "
31
 
32
  interface = gr.Interface(
33
  fn=predict,
34
  inputs=input,
35
- theme="grass",
36
- outputs= output,
37
  title=title,
38
 
39
  )
 
1
  import gradio as gr
2
  from transformers import pipeline
 
3
  import warnings
4
  import logging
5
  warnings.simplefilter('ignore')
6
  logging.disable(logging.WARNING)
7
 
8
+
9
 
10
 
11
  def predict(image):
12
  cap = pipeline('image-to-text')
13
+ caption = cap(image)
14
+
15
 
16
+ def sentiment_analysis(shortened):
 
17
  pipe = pipeline('text-classification')
18
+ senti = pipe(shortened)
19
+ return senti
20
+ caption_string = str(caption)
21
+ shortened = caption_string.replace("[{'generated_text': '" , "The image is of, ")
22
+ shortened = shortened.replace("'}]" , "")
23
+ sentiment = sentiment_analysis(shortened)
24
+ sentiment_string = ''.join(str(e) for e in sentiment)
25
+ formated_senti = sentiment_string.replace("{'label': ", ", The Tone of the image(sentiment) after analysisng the caption is that is it is ")
26
+ formated_senti = formated_senti.replace("'score': ", "in nature with an average percentage of ")
27
+ output = shortened + formated_senti[0:-2] + '%'
28
  return output
29
 
30
 
 
32
  label="Upload your Image and wait for 8-12 seconds!", type='pil', optional=False)
33
  output = gr.outputs.Textbox(label="Captions")
34
 
35
+ title = "Content-Mod API UI "
36
 
37
  interface = gr.Interface(
38
  fn=predict,
39
  inputs=input,
40
+ outputs=output,
 
41
  title=title,
42
 
43
  )