Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Cell 1: Image Classification Model
|
6 |
+
image_pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
|
7 |
+
|
8 |
+
def predict_image(input_img):
|
9 |
+
predictions = image_pipeline(input_img)
|
10 |
+
return input_img, {p["label"]: p["score"] for p in predictions}
|
11 |
+
|
12 |
+
image_gradio_app = gr.Interface(
|
13 |
+
fn=predict_image,
|
14 |
+
inputs=gr.Image(label="Select hot dog candidate", sources=['upload', 'webcam'], type="pil"),
|
15 |
+
outputs=[gr.Image(label="Processed Image"), gr.Label(label="Result", num_top_classes=2)],
|
16 |
+
title="Hot Dog? Or Not?",
|
17 |
+
)
|
18 |
+
|
19 |
+
# Cell 2: Chatbot Model
|
20 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
21 |
+
chatbot_model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
22 |
+
|
23 |
+
def predict_chatbot(input, history=[]):
|
24 |
+
new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')
|
25 |
+
bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
|
26 |
+
history = chatbot_model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id).tolist()
|
27 |
+
response = tokenizer.decode(history[0]).split("")
|
28 |
+
|
29 |
+
response_tuples = [(response[i], response[i+1]) for i in range(0, len(response)-1, 2)]
|
30 |
+
return response_tuples, history
|
31 |
+
|
32 |
+
chatbot_gradio_app = gr.Interface(
|
33 |
+
fn=predict_chatbot,
|
34 |
+
inputs=gr.Textbox(show_label=False, placeholder="Enter text and press enter"),
|
35 |
+
outputs=gr.Textbox(),
|
36 |
+
live=True,
|
37 |
+
title="Chatbot",
|
38 |
+
)
|
39 |
+
|
40 |
+
|
41 |
+
# Combine both interfaces into a single app
|
42 |
+
gr.TabbedInterface(
|
43 |
+
[image_gradio_app, chatbot_gradio_app],
|
44 |
+
tab_names=["image","chatbot"]
|
45 |
+
).launch()
|