GreenGreta / app.py
paloma99's picture
Update app.py
972c635 verified
raw
history blame
1.78 kB
import gradio as gr
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
import torch
# Image Classification Model
image_pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
def predict_image(input_img):
predictions = image_pipeline(input_img)
return input_img, {p["label"]: p["score"] for p in predictions}
image_gradio_app = gr.Interface(
fn=predict_image,
inputs=gr.Image(label="Select hot dog candidate", sources=['upload', 'webcam'], type="pil"),
outputs=[gr.Image(label="Processed Image"), gr.Label(label="Result", num_top_classes=2)],
title="Hot Dog? Or Not?",
)
# Chatbot Model
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
chatbot_model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
def predict_chatbot(input, history=[]):
new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')
bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
history = chatbot_model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id).tolist()
response = tokenizer.decode(history[0]).split("")
response_tuples = [(response[i], response[i+1]) for i in range(0, len(response)-1, 2)]
return response_tuples, history
chatbot_gradio_app = gr.Blocks()
with chatbot_gradio_app as demo:
chatbot = gr.Chatbot()
state = gr.State([])
with gr.Row():
txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter")
txt.submit(predict_chatbot, [txt, state], [chatbot, state])
# Launch both interfaces
if __name__ == "__main__":
gr.Interface(
columns=2,
children=[image_gradio_app, chatbot_gradio_app]
).launch()