Jiangxz commited on
Commit
e7ae0df
·
verified ·
1 Parent(s): bfa9b3b

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +244 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,244 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ # 財政部財政資訊中心 江信宗
3
+
4
+ import gradio as gr
5
+ from groq import Groq
6
+ import base64
7
+ import os
8
+ import io
9
+ import json
10
+ from PIL import Image
11
+ from zhconv_rs import zhconv
12
+ import traceback
13
+
14
+ custom_css = """
15
+ .center-aligned {
16
+ text-align: center !important;
17
+ color: #ff4081;
18
+ text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
19
+ }
20
+ .input-background {
21
+ background-color: #B7E0FF !important;
22
+ padding: 15px !important;
23
+ border-radius: 10px !important;
24
+ margin: 0 !important;
25
+ }
26
+ .input-background textarea {
27
+ font-size: 18px !important;
28
+ background-color: #ffffff;
29
+ border: 1px solid #f0f8ff;
30
+ border-radius: 8px;
31
+ }
32
+ .image-background {
33
+ border-radius: 10px !important;
34
+ border: 2px solid #B7E0FF !important;
35
+ }
36
+ .lng-background {
37
+ background-color: #FFF5CD !important;
38
+ padding: 5px !important;
39
+ border-radius: 10px !important;
40
+ margin: 0 !important;
41
+ }
42
+ .api-background {
43
+ background-color: #FFCFB3 !important;
44
+ padding: 5px !important;
45
+ border-radius: 10px !important;
46
+ margin: 0 !important;
47
+ }
48
+ .script-background {
49
+ background-color: #FEF9D9 !important;
50
+ padding: 15px !important;
51
+ border-radius: 10px !important;
52
+ margin: 0 !important;
53
+ }
54
+ .script-background textarea {
55
+ font-size: 18px !important;
56
+ background-color: #ffffff;
57
+ border: 1px solid #f0f8ff;
58
+ border-radius: 8px;
59
+ }
60
+ .model-background {
61
+ background-color: #FFF4B5 !important;
62
+ padding: 5px !important;
63
+ border-radius: 10px !important;
64
+ margin: 0 !important;
65
+ }
66
+ .model-background textarea {
67
+ font-size: 18px !important;
68
+ background-color: #ffffff;
69
+ border: 1px solid #f0f8ff;
70
+ border-radius: 8px;
71
+ }
72
+ .gen-button {
73
+ border-radius: 10px !important;
74
+ border: none !important;
75
+ background-color: #ff4081 !important;
76
+ color: white !important;
77
+ font-weight: bold !important;
78
+ transition: all 0.3s ease !important;
79
+ margin: 0 !important;
80
+ }
81
+ .gen-button:hover {
82
+ background-color: #f50057 !important;
83
+ transform: scale(1.05);
84
+ }
85
+ .clear-button {
86
+ color: white !important;
87
+ background-color: #000000 !important;
88
+ padding: 5px !important;
89
+ border-radius: 10px !important;
90
+ margin: 0 !important;
91
+ }
92
+ .clear-button:hover {
93
+ background-color: #000000 !important;
94
+ transform: scale(1.05);
95
+ }
96
+ """
97
+
98
+ MODELS = [
99
+ "llama-3.2-90b-vision-preview",
100
+ "llama-3.2-11b-vision-preview",
101
+ "llava-v1.5-7b-4096-preview"
102
+ ]
103
+
104
+ def compress_image(image, max_size=(800, 800), quality=95):
105
+ img = Image.open(image) if isinstance(image, str) else image
106
+ img.thumbnail(max_size)
107
+ buffered = io.BytesIO()
108
+ img.save(buffered, format="JPEG", quality=quality)
109
+ return buffered.getvalue()
110
+
111
+ def encode_image(image):
112
+ if isinstance(image, Image.Image):
113
+ buffered = io.BytesIO()
114
+ image.save(buffered, format="JPEG", quality=95)
115
+ return base64.b64encode(buffered.getvalue()).decode('utf-8')
116
+ else:
117
+ compressed = compress_image(image)
118
+ return base64.b64encode(compressed).decode('utf-8')
119
+
120
+ def create_client(api_key=None):
121
+ if not api_key:
122
+ api_key = os.getenv("YOUR_API_KEY")
123
+ return Groq(api_key=api_key)
124
+
125
+ def analyze_input(text_input, Quick_Input, Language):
126
+ if Quick_Input == "自行輸入":
127
+ return text_input.strip()
128
+ elif Quick_Input == "描述圖片":
129
+ if Language == "English":
130
+ Input_Text = "Take a close look at the image and describe it in as much detail as possible. Be sure to mention the main subject, the background, the colors used, the mood or feeling it evokes, and any specific elements that stand out."
131
+ else:
132
+ Input_Text = "仔細觀察圖片,依據圖片意境為圖片命名,並盡可能詳細的描述其內容。務必提及主體、背景、使用的顏色、所引發的情緒或感受,以及任何突出的特定元素。"
133
+ if text_input.strip():
134
+ Input_Text += " " + text_input.strip()
135
+ return Input_Text.strip()
136
+ elif Quick_Input == "圖片文字檢索":
137
+ if Language == "English":
138
+ Input_Text = "What does the text in this photo say?"
139
+ else:
140
+ Input_Text = "根據圖片中寫得文字內容是什麼?詳細列出所有文字。"
141
+ return Input_Text.strip()
142
+ elif Quick_Input == "圖像推理":
143
+ if Language == "English":
144
+ Input_Text = "Let's work this out in a step by step way to be sure we have the right answer. Deduce from the image and provide a quick answer."
145
+ else:
146
+ Input_Text = "讓我們一步一步地解決這個問題,以確保我們得到正確的答案。根據圖片進行推理並提供答案。"
147
+ if text_input.strip():
148
+ Input_Text += " " + text_input.strip()
149
+ return Input_Text.strip()
150
+ else:
151
+ return text_input.strip()
152
+
153
+ def process_image_and_text(image, text_input, Quick_Input, Language, model, api_key):
154
+ gr.Info("圖片正在分析中,請稍待片刻......")
155
+ if Quick_Input == "自行輸入" and not text_input.strip():
156
+ return "錯誤:請輸入問題或選擇快速輸入選項!!"
157
+ if not Language:
158
+ Language = "English"
159
+ text_input = text_input.strip()
160
+ client = create_client(api_key)
161
+ base64_image = encode_image(image)
162
+ Input_Text = analyze_input(text_input, Quick_Input, Language)
163
+ try:
164
+ chat_completion = client.chat.completions.create(
165
+ messages=[
166
+ {
167
+ "role": "user",
168
+ "content": [
169
+ {"type": "text", "text": Input_Text},
170
+ {
171
+ "type": "image_url",
172
+ "image_url": {
173
+ "url": f"data:image/jpeg;base64,{base64_image}",
174
+ },
175
+ },
176
+ ],
177
+ }
178
+ ],
179
+ model=model,
180
+ temperature=1,
181
+ )
182
+ gr.Info("Vision 模型已完成回答。")
183
+ return zhconv(chat_completion.choices[0].message.content.strip(), "zh-tw")
184
+ except Exception as e:
185
+ error_traceback = traceback.format_exc()
186
+ gr.Warning(f"發生錯誤:{error_traceback}")
187
+ return f"發生錯誤:{error_traceback}"
188
+
189
+ with gr.Blocks(theme=gr.themes.Monochrome(), css=custom_css) as iface:
190
+ gr.Markdown("""
191
+ # 🐹 Large Multimodal Models - 財政部財政資訊中心 🐹
192
+ > ### **※ 玩轉視覺推理與分析,探索我們的未來藍圖,系統布署:江信宗,Meta開源模型:Llama-3.2-90B-Vision。**
193
+ """, elem_classes="center-aligned")
194
+
195
+ with gr.Row():
196
+ with gr.Column(scale=1):
197
+ image_input = gr.Image(type="pil", label="上傳圖片", elem_classes="image-background")
198
+ model_select = gr.Dropdown(choices=MODELS, label="選擇 Vision 模型", value=MODELS[0], elem_classes="model-background")
199
+ api_key = gr.Textbox(label="請輸入您的 API Key", type="password", placeholder="API authentication key", elem_classes="api-background")
200
+ clear_button = gr.Button("清除回答及圖片", variant="secondary", elem_classes="clear-button")
201
+ gr.Markdown("""
202
+ ### **※ 可推理分析影片每秒每幀搜尋特定人事物所在時間軸。**
203
+ """, elem_classes="center-aligned")
204
+
205
+ with gr.Column(scale=1):
206
+ text_input = gr.Textbox(label="輸入問題", placeholder="請輸入您的問題...", autofocus=True, elem_classes="input-background", max_lines=5)
207
+ with gr.Row():
208
+ Quick_Input = gr.Dropdown(
209
+ choices=["自行輸入", "描述圖片", "圖像推理", "圖片文字檢索"],
210
+ value="自行輸入",
211
+ label="快速輸入",
212
+ interactive=True,
213
+ elem_classes="lng-background"
214
+ )
215
+ Language = gr.Dropdown(
216
+ choices=["繁體中文", "English"],
217
+ value="繁體中文",
218
+ label="回答語言",
219
+ interactive=True,
220
+ elem_classes="lng-background"
221
+ )
222
+ submit_button = gr.Button("傳送", variant="primary", elem_classes="gen-button")
223
+ output = gr.Textbox(label="Vision 模型回答", elem_classes="script-background", max_lines=40)
224
+
225
+ submit_button.click(
226
+ fn=process_image_and_text,
227
+ inputs=[image_input, text_input, Quick_Input, Language, model_select, api_key],
228
+ outputs=[output]
229
+ )
230
+
231
+ def clear_outputs():
232
+ return None, None, ""
233
+
234
+ clear_button.click(
235
+ fn=clear_outputs,
236
+ inputs=[],
237
+ outputs=[image_input, text_input, output]
238
+ )
239
+
240
+ if __name__ == "__main__":
241
+ if "SPACE_ID" in os.environ:
242
+ iface.launch()
243
+ else:
244
+ iface.launch(share=True, show_api=False)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ groq
3
+ zhconv_rs