Spaces:
Running
on
Zero
Running
on
Zero
Upload 2 files
Browse files- module_chat.py +36 -0
- module_translation.py +115 -0
module_chat.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
File: module_chat.py
|
3 |
+
Description: A module for chat using text (+images) with a multimodal interface.
|
4 |
+
Author: Didier Guillevic
|
5 |
+
Date: 2025-03-16
|
6 |
+
"""
|
7 |
+
|
8 |
+
import gradio as gr
|
9 |
+
import vlm
|
10 |
+
|
11 |
+
def process(message, history):
|
12 |
+
"""Generate the model response given message and history
|
13 |
+
"""
|
14 |
+
messages = vlm.build_messages(message, history)
|
15 |
+
yield from vlm.stream_response(messages)
|
16 |
+
|
17 |
+
#
|
18 |
+
# User interface
|
19 |
+
#
|
20 |
+
with gr.Blocks() as demo:
|
21 |
+
chat_interface = gr.ChatInterface(
|
22 |
+
fn=process,
|
23 |
+
#description="Chat with text or text+image.",
|
24 |
+
multimodal=True,
|
25 |
+
examples=[
|
26 |
+
"How can we rationalize quantum entanglement?",
|
27 |
+
"Peux-tu expliquer le terme 'quantum spin'?",
|
28 |
+
{'files': ['./sample_ID.jpeg',], 'text': 'Describe this image in a few words.'},
|
29 |
+
{
|
30 |
+
'files': ['./sample_ID.jpeg',],
|
31 |
+
'text': (
|
32 |
+
'Could you extract the information present in the image '
|
33 |
+
'and present it as a bulleted list?')
|
34 |
+
},
|
35 |
+
]
|
36 |
+
)
|
module_translation.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
File: module_translation.py
|
3 |
+
Description: Translating text with a multimodal interface.
|
4 |
+
Author: Didier Guillevic
|
5 |
+
Date: 2025-03-16
|
6 |
+
"""
|
7 |
+
|
8 |
+
import gradio as gr
|
9 |
+
import vlm
|
10 |
+
from deep_translator import GoogleTranslator
|
11 |
+
|
12 |
+
tgt_language_codes = {
|
13 |
+
'English': 'en',
|
14 |
+
'French': 'fr'
|
15 |
+
}
|
16 |
+
code_to_languages = {v: k for k, v in tgt_language_codes.items()}
|
17 |
+
|
18 |
+
translation_prompt = (
|
19 |
+
"Please translate the following text into {}. "
|
20 |
+
"Do not include any other information. "
|
21 |
+
"Output only the translation of the given text. "
|
22 |
+
"Text:\n\n{}"
|
23 |
+
)
|
24 |
+
|
25 |
+
def translate_text(text, tgt_lang):
|
26 |
+
"""Translate the given text into the given target language.
|
27 |
+
"""
|
28 |
+
# Build messages
|
29 |
+
messages = [
|
30 |
+
{
|
31 |
+
'role': 'user',
|
32 |
+
'content': [
|
33 |
+
{
|
34 |
+
"type": "text",
|
35 |
+
"text": translation_prompt.format(
|
36 |
+
code_to_languages[tgt_lang], text)
|
37 |
+
}
|
38 |
+
]
|
39 |
+
}
|
40 |
+
]
|
41 |
+
|
42 |
+
# Get the translation
|
43 |
+
translated_text = vlm.get_response(messages)
|
44 |
+
translated_text_google = GoogleTranslator(
|
45 |
+
source='auto', target=tgt_lang).translate(text=text)
|
46 |
+
|
47 |
+
return translated_text, translated_text_google
|
48 |
+
|
49 |
+
#
|
50 |
+
# User interface
|
51 |
+
#
|
52 |
+
with gr.Blocks() as demo:
|
53 |
+
with gr.Row():
|
54 |
+
input_text = gr.Textbox(
|
55 |
+
lines=5,
|
56 |
+
placeholder="Enter text to translate",
|
57 |
+
label="Text to translate",
|
58 |
+
render=True
|
59 |
+
)
|
60 |
+
|
61 |
+
with gr.Row():
|
62 |
+
output_text = gr.Textbox(
|
63 |
+
lines=5,
|
64 |
+
label=vlm.model_id,
|
65 |
+
render=True
|
66 |
+
)
|
67 |
+
output_text_google = gr.Textbox(
|
68 |
+
lines=5,
|
69 |
+
label="Google Translate",
|
70 |
+
render=True
|
71 |
+
)
|
72 |
+
|
73 |
+
with gr.Row():
|
74 |
+
tgt_lang = gr.Dropdown(
|
75 |
+
choices=tgt_language_codes.items(),
|
76 |
+
value="en",
|
77 |
+
label="Target language",
|
78 |
+
render=True
|
79 |
+
)
|
80 |
+
translate_btn = gr.Button(value="Translate", variant="primary")
|
81 |
+
clear_btn = gr.Button("Clear", variant="secondary")
|
82 |
+
|
83 |
+
# Examples
|
84 |
+
examples = gr.Examples(
|
85 |
+
[
|
86 |
+
["ریچارد مور، رئیس سازمان مخفی اطلاعاتی بریتانیا (امآی۶) در دیدار ویلیام برنز، رئیس سازمان اطلاعات مرکزی آمریکا (سیا) گفت همچنان احتمال اقدام ایران علیه اسرائیل در واکنش به ترور اسماعیل هنیه، رهبر حماس وجود دارد. آقای برنز نیز در این دیدار فاش کرد که در سال اول جنگ اوکراین، «خطر واقعی» وجود داشت که روسیه به استفاده از «تسلیحات هستهای تاکتیکی» متوسل شود. این دو مقام امنیتی هشدار دادند که «نظم جهانی» از زمان جنگ سرد تا کنون تا این حد «در معرض تهدید» نبوده است.", "en"],
|
87 |
+
["Clément Delangue est, avec Julien Chaumond et Thomas Wolf, l’un des trois Français cofondateurs de Hugging Face, une start-up d’intelligence artificielle (IA) de premier plan. Valorisée à 4,2 milliards d’euros après avoir levé près de 450 millions d’euros depuis sa création en 2016, cette société de droit américain est connue comme la plate-forme de référence où développeurs et entreprises publient des outils et des modèles pour faire de l’IA en open source, c’est-à-dire accessible gratuitement et modifiable.", "en"],
|
88 |
+
["يُعد تفشي مرض جدري القردة قضية صحية عالمية خطيرة، ومن المهم محاولة منع انتشاره للحفاظ على سلامة الناس وتجنب العدوى. د. صموئيل بولاند، مدير الحوادث الخاصة بمرض الجدري في المكتب الإقليمي لمنظمة الصحة العالمية في أفريقيا، يتحدث من كينشاسا في جمهورية الكونغو الديمقراطية، ولديه بعض النصائح البسيطة التي يمكن للناس اتباعها لتقليل خطر انتشار المرض.", "en"],
|
89 |
+
["【ワシントン=冨山優介】米ボーイングの新型宇宙船「スターライナー」は7日午前0時(日本時間7日午後1時)過ぎ、米ニューメキシコ州のホワイトサンズ宇宙港に着地し、地球に帰還した。スターライナーは米宇宙飛行士2人を乗せて6月に打ち上げられ、国際宇宙ステーション(ISS)に接続したが、機体のトラブルが解決できず、無人でISSから離脱した。", "en"],
|
90 |
+
["張先生稱,奇瑞已經凖備在西班牙生產汽車,並決心採取「本地化」的方式進入歐洲市場。此外,他也否認該公司的出口受益於不公平補貼。奇瑞成立於1997年,是中國最大的汽車公司之一。它已經是中國最大的汽車出口商,並且制定了進一步擴張的野心勃勃的計劃。", "en"],
|
91 |
+
["ברוכה הבאה, קיטי: בית הקפה החדש בלוס אנג'לס החתולה האהובה והחברים שלה מקבלים בית קפה משלהם בשדרות יוניברסל סיטי, שם תוכלו למצוא מגוון של פינוקים מתוקים – החל ממשקאות ועד עוגות", "en"],
|
92 |
+
],
|
93 |
+
inputs=[input_text, tgt_lang],
|
94 |
+
outputs=[output_text, output_text_google],
|
95 |
+
fn=translate_text,
|
96 |
+
cache_examples=False,
|
97 |
+
label="Examples"
|
98 |
+
)
|
99 |
+
|
100 |
+
# Click actions
|
101 |
+
translate_btn.click(
|
102 |
+
fn=translate_text,
|
103 |
+
inputs=[input_text, tgt_lang],
|
104 |
+
outputs=[output_text, output_text_google]
|
105 |
+
)
|
106 |
+
clear_btn.click(
|
107 |
+
fn=lambda : ('', '', ''), # input_text, output_text, output_text_google
|
108 |
+
inputs=[],
|
109 |
+
outputs=[input_text, output_text, output_text_google]
|
110 |
+
)
|
111 |
+
|
112 |
+
with gr.Accordion("Documentation", open=False):
|
113 |
+
gr.Markdown(f"""
|
114 |
+
- Model: serving {vlm.model_id} and Google Translate.
|
115 |
+
""")
|