suryabbrj's picture
fixed some parsing issues, optimized payload delivery to reduce prediction time
9d5c1d8
raw
history blame
1.27 kB
import gradio as gr
from transformers import pipeline
import warnings
import logging
warnings.simplefilter('ignore')
logging.disable(logging.WARNING)
def predict(image):
cap = pipeline('image-to-text')
caption = cap(image)
def sentiment_analysis(shortened):
pipe = pipeline('text-classification')
senti = pipe(shortened)
return senti
caption_string = str(caption)
shortened = caption_string.replace("[{'generated_text': '" , "The image is of, ")
shortened = shortened.replace("'}]" , "")
sentiment = sentiment_analysis(shortened)
sentiment_string = ''.join(str(e) for e in sentiment)
formated_senti = sentiment_string.replace("{'label': ", ", The Tone of the image(sentiment) after analysisng the caption is that is it is ")
formated_senti = formated_senti.replace("'score': ", "in nature with an average percentage of ")
output = shortened + formated_senti[0:-2] + '%'
return output
input = gr.inputs.Image(
label="Upload your Image and wait for 8-12 seconds!", type='pil', optional=False)
output = gr.outputs.Textbox(label="Captions")
title = "Content-Mod API UI "
interface = gr.Interface(
fn=predict,
inputs=input,
outputs=output,
title=title,
)
interface.launch()