Didier commited on
Commit
73f064f
·
verified ·
1 Parent(s): 52e2382

Upload 5 files

Browse files
Files changed (6) hide show
  1. .gitattributes +1 -0
  2. module_chat.py +40 -0
  3. module_rewriting.py +157 -0
  4. module_translation.py +116 -0
  5. sample_ID.jpeg +3 -0
  6. vlm.py +103 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ sample_ID.jpeg filter=lfs diff=lfs merge=lfs -text
module_chat.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ return vlm.get_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
+ )
37
+
38
+ if __name__ == "__main__":
39
+ demo.launch()
40
+
module_rewriting.py ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ File: module_rewriting.py
3
+ Description: Rewrite some given text in a given style and language.
4
+ Author: Didier Guillevic
5
+ Date: 2025-03-16
6
+ """
7
+
8
+ import gradio as gr
9
+ import vlm
10
+
11
+ tgt_language_codes = {
12
+ 'English': 'en',
13
+ 'French': 'fr'
14
+ }
15
+ code_to_languages = {v: k for k, v in tgt_language_codes.items()}
16
+
17
+ #
18
+ # Examples of bad writing: https://lafavephilosophy.x10host.com/writsamp0.htm
19
+ #
20
+
21
+ example_bad_writing_2 = (
22
+ "Existing is being unique. Existence, reality, essence, cause, or truth is uniqueness. "
23
+ "The geometric point in the center of the sphere is nature’s symbol of the immeasurable "
24
+ "uniqueness within its measurable effect. "
25
+ "A center is always unique; otherwise it would not be a center. "
26
+ "Because uniqueness is reality, or that which makes a thing what it is, "
27
+ "everything that is real is based on a centralization."
28
+ )
29
+ example_bad_writing_3 = (
30
+ "The amount of grammer and usage error’s today is astounding. "
31
+ "Not to mention spelling. If I was a teacher, I’d feel badly "
32
+ "that less and less students seem to understand the basic principals "
33
+ "of good writing. Neither the oldest high school students nor the "
34
+ "youngest kindergartner know proper usage. "
35
+ "A student often thinks they can depend on word processing programs "
36
+ "to correct they’re errors. Know way!"
37
+ "Watching TV all the time, its easy to see why their having trouble. "
38
+ "TV interferes with them studying and it’s strong affect on children "
39
+ "has alot to due with their grades. There’s other factors, too, "
40
+ "including the indifference of parents like you and I. "
41
+ "A Mom or Dad often doesn’t know grammer themselves. "
42
+ "We should tell are children to study hard like we did at "
43
+ "they’re age and to watch less TV then their classmates."
44
+ )
45
+ example_bad_writing_9 = (
46
+ "Immanuel Kant was a great philosipher that came up with many "
47
+ "philosophical thoughts. He represents philosophy at it’s best. "
48
+ "One issue that went against his moral laws was that of people "
49
+ "having a lack of honesty or lying. Kant was strongly in favor of "
50
+ "the view that when the ethical and moral decision to lie is made "
51
+ "by a person, they’re would always be negative consequences of "
52
+ "they’re choice. "
53
+ "Kant also held the firm belief that lying was wrong at all times. "
54
+ "I disagree, my view is that sometimes all lying is not wrong."
55
+ )
56
+
57
+ rewrite_prompt = (
58
+ "{} "
59
+ "Respond exclusively using the {} language. "
60
+ "Text:\n\n{}"
61
+ )
62
+
63
+ def rewrite_text(text, instruction, tgt_lang):
64
+ """Rewrite the given text in the given target language.
65
+ """
66
+ # Build messages
67
+ messages = [
68
+ {
69
+ 'role': 'user',
70
+ 'content': [
71
+ {
72
+ "type": "text",
73
+ "text": rewrite_prompt.format(
74
+ instruction, code_to_languages[tgt_lang], text)
75
+ }
76
+ ]
77
+ }
78
+ ]
79
+ return vlm.get_response(messages)
80
+
81
+ #
82
+ # User interface
83
+ #
84
+ with gr.Blocks() as demo:
85
+ with gr.Row():
86
+ input_text = gr.Textbox(
87
+ lines=5,
88
+ placeholder="Enter text to rewrite",
89
+ label="Text to rewrite",
90
+ render=True
91
+ )
92
+ output_text = gr.Textbox(
93
+ lines=5,
94
+ label="Rewritten text",
95
+ render=True
96
+ )
97
+
98
+ with gr.Row():
99
+ tgt_lang = gr.Dropdown(
100
+ choices=tgt_language_codes.items(),
101
+ value="en",
102
+ label="Target language",
103
+ render=True,
104
+ scale=1
105
+ )
106
+ instruction = gr.Textbox(
107
+ lines=1,
108
+ value="Rewrite the following text in a more professional style.",
109
+ label="Instruction",
110
+ render=True,
111
+ scale=4
112
+ )
113
+
114
+ with gr.Row():
115
+ rewrite_btn = gr.Button(value="Rewrite", variant="primary")
116
+ clear_btn = gr.Button("Clear", variant="secondary")
117
+
118
+ # Examples
119
+ with gr.Accordion("Examples", open=False):
120
+ examples = gr.Examples(
121
+ [
122
+ ["Howdy mate! Wanna grab a bite?", ],
123
+ [example_bad_writing_3, ],
124
+ [example_bad_writing_2, ],
125
+ [ ("The work wa really not that great. "
126
+ "They simply surfed the web to find the solution to their problem."),
127
+ ],
128
+ ["Ils ont rien foutus. Ils sont restés assis sur leur postérieur toute la journée.", ],
129
+ ],
130
+ inputs=[input_text, instruction, tgt_lang],
131
+ outputs=[output_text,],
132
+ fn=rewrite_text,
133
+ cache_examples=False,
134
+ label="Examples"
135
+ )
136
+
137
+ # Documentation
138
+ with gr.Accordion("Documentation", open=False):
139
+ gr.Markdown(f"""
140
+ - Model: {vlm.model_id}.
141
+ """)
142
+
143
+ # Click actions
144
+ rewrite_btn.click(
145
+ fn=rewrite_text,
146
+ inputs=[input_text, instruction, tgt_lang],
147
+ outputs=[output_text,]
148
+ )
149
+ clear_btn.click(
150
+ fn=lambda : ('', ''), # input_text, output_text, output_text_google
151
+ inputs=[],
152
+ outputs=[input_text, output_text]
153
+ )
154
+
155
+
156
+ if __name__ == "__main__":
157
+ demo.launch()
module_translation.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ with gr.Accordion("Examples", open=False):
85
+ examples = gr.Examples(
86
+ [
87
+ ["ریچارد مور، رئیس سازمان مخفی اطلاعاتی بریتانیا (ام‌آی‌۶) در دیدار ویلیام برنز، رئیس سازمان اطلاعات مرکزی آمریکا (سیا) گفت همچنان احتمال اقدام ایران علیه اسرائیل در واکنش به ترور اسماعیل هنیه، رهبر حماس وجود دارد. آقای برنز نیز در این دیدار فاش کرد که در سال اول جنگ اوکراین، «خطر واقعی» وجود داشت که روسیه به استفاده از «تسلیحات هسته‌ای تاکتیکی» متوسل شود. این دو مقام امنیتی هشدار دادند که «نظم جهانی» از زمان جنگ سرد تا کنون تا این حد «در معرض تهدید» نبوده است.", "en"],
88
+ ["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"],
89
+ ["يُعد تفشي مرض جدري القردة قضية صحية عالمية خطيرة، ومن المهم محاولة منع انتشاره للحفاظ على سلامة الناس وتجنب العدوى. د. صموئيل بولاند، مدير الحوادث الخاصة بمرض الجدري في المكتب الإقليمي لمنظمة الصحة العالمية في أفريقيا، يتحدث من كينشاسا في جمهورية الكونغو الديمقراطية، ولديه بعض النصائح البسيطة التي يمكن للناس اتباعها لتقليل خطر انتشار المرض.", "en"],
90
+ ["【ワシントン=冨山優介】米ボーイングの新型宇宙船「スターライナー」は7日午前0時(日本時間7日午後1時)過ぎ、米ニューメキシコ州のホワイトサンズ宇宙港に着地し、地球に帰還した。スターライナーは米宇宙飛行士2人を乗せて6月に打ち上げられ、国際宇宙ステーション(ISS)に接続したが、機体のトラブルが解決できず、無人でISSから離脱した。", "en"],
91
+ ["張先生稱,奇瑞已經凖備在西班牙生產汽車,並決心採取「本地化」的方式進入歐洲市場。此外,他也否認該公司的出口受益於不公平補貼。奇瑞成立於1997年,是中國最大的汽車公司之一。它已經是中國最大的汽車出口商,並且制定了進一步擴張的野心勃勃的計劃。", "en"],
92
+ ["ברוכה הבאה, קיטי: בית הקפה החדש בלוס אנג'לס החתולה האהובה והחברים שלה מקבלים בית קפה משלהם בשדרות יוניברסל סיטי, שם תוכלו למצוא מגוון של פינוקים מתוקים – החל ממשקאות ועד עוגות", "en"],
93
+ ],
94
+ inputs=[input_text, tgt_lang],
95
+ outputs=[output_text, output_text_google],
96
+ fn=translate_text,
97
+ cache_examples=False,
98
+ label="Examples"
99
+ )
100
+
101
+ # Click actions
102
+ translate_btn.click(
103
+ fn=translate_text,
104
+ inputs=[input_text, tgt_lang],
105
+ outputs=[output_text, output_text_google]
106
+ )
107
+ clear_btn.click(
108
+ fn=lambda : ('', '', ''), # input_text, output_text, output_text_google
109
+ inputs=[],
110
+ outputs=[input_text, output_text, output_text_google]
111
+ )
112
+
113
+ with gr.Accordion("Documentation", open=False):
114
+ gr.Markdown(f"""
115
+ - Model: serving {vlm.model_id} and Google Translate.
116
+ """)
sample_ID.jpeg ADDED

Git LFS Details

  • SHA256: 61666983ee2654df2c51a2493acea1013955fc4836da2143f668cc077fdbe46a
  • Pointer size: 131 Bytes
  • Size of remote file: 202 kB
vlm.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """ vlm.py
2
+
3
+ Utilities for working with Vision Language Models
4
+
5
+ :author: Didier Guillevic
6
+ :email: [email protected]
7
+ :creation: 2024-12-28
8
+ """
9
+
10
+ import logging
11
+ logger = logging.getLogger(__name__)
12
+ logging.basicConfig(level=logging.INFO)
13
+
14
+ import os
15
+ from mistralai import Mistral
16
+ import base64
17
+
18
+ #
19
+ # Mistral AI client
20
+ #
21
+ api_key = os.environ["MISTRAL_API_KEY"]
22
+ client = Mistral(api_key=api_key)
23
+ model_id = "mistral-small-latest" # 128k context window
24
+
25
+ #
26
+ # Encode images as base64
27
+ #
28
+ def encode_image(image_path):
29
+ """Encode the image to base64."""
30
+ try:
31
+ with open(image_path, "rb") as image_file:
32
+ return base64.b64encode(image_file.read()).decode('utf-8')
33
+ except FileNotFoundError:
34
+ print(f"Error: The file {image_path} was not found.")
35
+ return None
36
+ except Exception as e: # Added general exception handling
37
+ print(f"Error: {e}")
38
+ return None
39
+
40
+
41
+ #
42
+ # Build messages
43
+ #
44
+ def build_messages(message: dict, history: list[tuple]):
45
+ """Build messages given message & history from a **multimodal** chat interface.
46
+
47
+ Args:
48
+ message: dictionary with keys: 'text', 'files'
49
+ history: list of tuples with (message, response)
50
+
51
+ Returns:
52
+ list of messages (to be sent to the model)
53
+ """
54
+ logger.info(f"{message=}")
55
+ logger.info(f"{history=}")
56
+ # Get the user's text and list of images
57
+ user_text = message.get("text", "")
58
+ user_images = message.get("files", []) # List of images
59
+
60
+ # Build the message list including history
61
+ messages = []
62
+ combined_user_input = [] #Combine images and text if found in same turn.
63
+ for user_turn, bot_turn in history:
64
+ if isinstance(user_turn, tuple): # Image input
65
+ image_content = [{"type": "image_url", "image_url": image_url} for image_url in user_turn]
66
+ combined_user_input.extend(image_content)
67
+ elif isinstance(user_turn, str): #Text input
68
+ combined_user_input.append({"type":"text", "text": user_turn})
69
+ if combined_user_input and bot_turn:
70
+ messages.append({'role': 'user', 'content': combined_user_input})
71
+ messages.append({'role': 'assistant', 'content': [{"type": "text", "text": bot_turn}]})
72
+ combined_user_input = [] #reset the combined user input.
73
+
74
+ # Build the user message's content from the provided message
75
+ user_content = []
76
+ if user_text:
77
+ user_content.append({"type": "text", "text": user_text})
78
+ for image in user_images:
79
+ user_content.append(
80
+ {
81
+ "type": "image_url",
82
+ "image_url": f"data:image/jpeg;base64,{encode_image(image)}"
83
+ }
84
+ )
85
+
86
+ messages.append({'role': 'user', 'content': user_content})
87
+ logger.info(f"{messages=}")
88
+
89
+ return messages
90
+
91
+ #
92
+ # get response
93
+ #
94
+ def get_response(messages: list[dict]):
95
+ """Stream the model's response to the chat interface.
96
+
97
+ Args:
98
+ messages: list of messages to send to the model
99
+ """
100
+ response = client.chat.complete(model=model_id, messages=messages)
101
+ logger.info(f"{response=}")
102
+ return response.choices[0].message.content
103
+