Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,61 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from gradio.mix import Parallel, Series
|
| 3 |
+
from gradio.blocks import Input, Output, Processor
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import speech_recognition as sr
|
| 6 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 7 |
+
|
| 8 |
+
# Load pre-trained model and tokenizer
|
| 9 |
+
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
| 10 |
+
model = GPT2LMHeadModel.from_pretrained("gpt2")
|
| 11 |
+
|
| 12 |
+
# Placeholder function to handle text input
|
| 13 |
+
def handle_text(text):
|
| 14 |
+
# encode the new user input, add the eos_token and return a tensor in Pytorch
|
| 15 |
+
new_user_input_ids = tokenizer.encode(text + tokenizer.eos_token, return_tensors='pt')
|
| 16 |
+
|
| 17 |
+
# append the new user input tokens to the chat history
|
| 18 |
+
bot_input_ids = new_user_input_ids
|
| 19 |
+
|
| 20 |
+
# generate a response
|
| 21 |
+
chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
|
| 22 |
+
|
| 23 |
+
# Print the generated chat
|
| 24 |
+
chat_output = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
| 25 |
+
return chat_output
|
| 26 |
+
|
| 27 |
+
# Placeholder function to handle image input
|
| 28 |
+
def handle_image(img):
|
| 29 |
+
# This is a placeholder function, replace with your own image processing function
|
| 30 |
+
return "This image seems nice!"
|
| 31 |
+
|
| 32 |
+
# Placeholder function to handle audio input
|
| 33 |
+
def handle_audio(audio):
|
| 34 |
+
# This is a placeholder function, replace with your own audio processing function
|
| 35 |
+
r = sr.Recognizer()
|
| 36 |
+
with sr.AudioFile(audio.name) as source:
|
| 37 |
+
audio_data = r.record(source)
|
| 38 |
+
text = r.recognize_google(audio_data)
|
| 39 |
+
return handle_text(text)
|
| 40 |
+
|
| 41 |
+
# Define the Gradio interface
|
| 42 |
+
iface = gr.Interface(
|
| 43 |
+
[
|
| 44 |
+
Parallel(
|
| 45 |
+
Input(type="text", label="Input Text"),
|
| 46 |
+
Input(type="image", label="Upload Image"),
|
| 47 |
+
Input(type="audio", label="Input Audio"),
|
| 48 |
+
),
|
| 49 |
+
Series(
|
| 50 |
+
Processor(handle_text, "text"),
|
| 51 |
+
Processor(handle_image, "image"),
|
| 52 |
+
Processor(handle_audio, "audio"),
|
| 53 |
+
Output(type="text", label="Output"),
|
| 54 |
+
)
|
| 55 |
+
],
|
| 56 |
+
title="Multimodal Chatbot",
|
| 57 |
+
description="This chatbot can handle text, image, and audio inputs. Try it out!",
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
# Launch the Gradio interface
|
| 61 |
+
iface.launch()
|