Princeaka commited on
Commit
4ce8649
ยท
verified ยท
1 Parent(s): 65beca9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -36
app.py CHANGED
@@ -1,51 +1,74 @@
1
  import os
2
- os.environ["HF_HUB_DISABLE_TELEMETRY"] = "1"
3
- os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
4
  import gradio as gr
5
  from multimodal_module import MultiModalChatModule
6
  import asyncio
7
 
8
- AI = MultiModalChatModule()
 
9
 
10
- async def chat_async(text, user_id, lang):
11
- return await AI.generate_response(text, int(user_id), lang)
 
12
 
13
- def chat(text, user_id, lang="en"):
14
- return asyncio.run(chat_async(text, user_id, lang))
 
15
 
16
- async def voice_async(audio, user_id):
17
- return await AI.process_voice_message(audio, int(user_id))
18
 
19
- def voice(audio, user_id):
20
- return asyncio.run(voice_async(audio, user_id))
21
 
22
- async def image_async(image, user_id):
23
- return await AI.process_image_message(image, int(user_id))
24
 
25
- def image(image, user_id):
26
- return asyncio.run(image_async(image, user_id))
 
 
 
 
27
 
28
- with gr.Blocks() as demo:
29
- gr.Markdown("# Multimodal Bot API")
30
 
31
- with gr.Tab("Text Chat"):
32
- t_in = gr.Textbox(label="Message")
33
- u_in = gr.Textbox(label="User ID", value="123")
34
- l_in = gr.Textbox(label="Language", value="en")
35
- t_out = gr.Textbox(label="AI Response")
36
- gr.Button("Send").click(chat, inputs=[t_in, u_in, l_in], outputs=t_out)
 
37
 
38
- with gr.Tab("Voice"):
39
- v_in = gr.Audio(sources=["microphone", "upload"], type="filepath")
40
- vu_in = gr.Textbox(label="User ID", value="123")
41
- v_out = gr.JSON(label="Voice Analysis")
42
- gr.Button("Process").click(voice, inputs=[v_in, vu_in], outputs=v_out)
43
 
44
- with gr.Tab("Image"):
45
- i_in = gr.Image(type="filepath")
46
- iu_in = gr.Textbox(label="User ID", value="123")
47
- i_out = gr.Textbox(label="Image Description")
48
- gr.Button("Describe").click(image, inputs=[i_in, iu_in], outputs=i_out)
49
-
50
- demo.queue()
51
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
 
 
2
  import gradio as gr
3
  from multimodal_module import MultiModalChatModule
4
  import asyncio
5
 
6
+ # Initialize module
7
+ mm = MultiModalChatModule()
8
 
9
+ # Environment configuration
10
+ os.environ["HF_HUB_DISABLE_TELEMETRY"] = "1"
11
+ os.environ["TOKENIZERS_PARALLELISM"] = "false"
12
 
13
+ async def async_wrapper(fn, *args):
14
+ """Handle async calls from Gradio"""
15
+ return await fn(*args)
16
 
17
+ def process_voice(audio, user_id):
18
+ return asyncio.run(async_wrapper(mm.process_voice_message, audio, int(user_id)))
19
 
20
+ def process_image(image, user_id):
21
+ return asyncio.run(async_wrapper(mm.process_image_message, image, int(user_id)))
22
 
23
+ def chat(text, user_id, lang):
24
+ return asyncio.run(async_wrapper(mm.generate_response, text, int(user_id), lang))
25
 
26
+ def generate_image(prompt, user_id):
27
+ return asyncio.run(async_wrapper(
28
+ mm.generate_image_from_text,
29
+ prompt,
30
+ int(user_id)
31
+ ))
32
 
33
+ with gr.Blocks(title="Multimodal AI Assistant") as app:
34
+ gr.Markdown("## ๐Ÿš€ Multimodal AI Assistant")
35
 
36
+ with gr.Tab("๐Ÿ’ฌ Text Chat"):
37
+ with gr.Row():
38
+ user_id = gr.Textbox(label="User ID", value="123")
39
+ lang = gr.Dropdown(["en", "es", "fr", "de"], label="Language", value="en")
40
+ chat_input = gr.Textbox(label="Your Message")
41
+ chat_output = gr.Textbox(label="AI Response", interactive=False)
42
+ chat_btn = gr.Button("Send")
43
 
44
+ with gr.Tab("๐ŸŽ™๏ธ Voice"):
45
+ voice_input = gr.Audio(sources=["microphone", "upload"], type="filepath")
46
+ voice_user = gr.Textbox(label="User ID", value="123")
47
+ voice_output = gr.JSON(label="Analysis Results")
48
+ voice_btn = gr.Button("Process")
49
 
50
+ with gr.Tab("๐Ÿ–ผ๏ธ Images"):
51
+ with gr.Tab("Describe"):
52
+ img_input = gr.Image(type="filepath")
53
+ img_user = gr.Textbox(label="User ID", value="123")
54
+ img_output = gr.Textbox(label="Description")
55
+ img_btn = gr.Button("Describe")
56
+
57
+ with gr.Tab("Generate"):
58
+ gen_prompt = gr.Textbox(label="Prompt")
59
+ gen_user = gr.Textbox(label="User ID", value="123")
60
+ gen_output = gr.Image(label="Generated Image")
61
+ gen_btn = gr.Button("Generate")
62
+
63
+ # Event handlers
64
+ chat_btn.click(chat, [chat_input, user_id, lang], chat_output)
65
+ voice_btn.click(process_voice, [voice_input, voice_user], voice_output)
66
+ img_btn.click(process_image, [img_input, img_user], img_output)
67
+ gen_btn.click(generate_image, [gen_prompt, gen_user], gen_output)
68
+
69
+ if __name__ == "__main__":
70
+ app.launch(
71
+ server_name="0.0.0.0",
72
+ server_port=7860,
73
+ share=False
74
+ )