HakimHa commited on
Commit
830721f
Β·
1 Parent(s): 4c8e8be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -45
app.py CHANGED
@@ -1,46 +1,61 @@
1
  import gradio as gr
2
- import time
3
-
4
- # Chatbot demo with multimodal input (text, markdown, LaTeX, code blocks, image, audio, & video). Plus shows support for streaming text.
5
-
6
- def add_text(history, text):
7
- history = history + [(text, None)]
8
- return history, gr.update(value="", interactive=False)
9
-
10
-
11
- def add_file(history, file):
12
- history = history + [((file.name,), None)]
13
- return history
14
-
15
-
16
- def bot(history):
17
- response = "**That's cool!**"
18
- history[-1][1] = ""
19
- for character in response:
20
- history[-1][1] += character
21
- time.sleep(0.05)
22
- yield history
23
-
24
-
25
- with gr.Blocks() as demo:
26
- chatbot = gr.Chatbot([], elem_id="chatbot").style(height=750)
27
-
28
- with gr.Row():
29
- with gr.Column(scale=0.85):
30
- txt = gr.Textbox(
31
- show_label=False,
32
- placeholder="Enter text and press enter, or upload an image",
33
- ).style(container=False)
34
- with gr.Column(scale=0.15, min_width=0):
35
- btn = gr.UploadButton("πŸ“", file_types=["image", "video", "audio"])
36
-
37
- txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
38
- bot, chatbot, chatbot
39
- )
40
- txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
41
- file_msg = btn.upload(add_file, [chatbot, btn], [chatbot], queue=False).then(
42
- bot, chatbot, chatbot
43
- )
44
-
45
- demo.queue()
46
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()