suryabbrj's picture
Update app.py
8ae1366
raw
history blame
1.35 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[21:-3]
formatted_capt = caption_string.replace("[{'generated_text': '" , "The image is of, ")
formatted_capt = formatted_capt.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 = formatted_capt + 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 with Sentiment-Analysis UI "
interface = gr.Interface(
fn=predict,
inputs=input,
outputs=output,
title=title,
)
interface.launch()