jerrychen428 commited on
Commit
fc15c86
·
verified ·
1 Parent(s): fad7b73

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +179 -0
app.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from groq import Groq
3
+ import base64
4
+ import io
5
+ from PIL import Image
6
+
7
+ def encode_image(image):
8
+ """將 PIL Image 編碼為 base64 字符串"""
9
+ buffered = io.BytesIO()
10
+ image.save(buffered, format="JPEG")
11
+ return base64.b64encode(buffered.getvalue()).decode("utf-8")
12
+
13
+ def analyze_image(api_key, image, prompt, temperature=1, max_tokens=512):
14
+ """使用 Groq API 分析圖片"""
15
+ try:
16
+ # 檢查 API Key 是否為空
17
+ if not api_key or api_key.strip() == "":
18
+ return "錯誤:請輸入有效的 Groq API Key"
19
+
20
+ # 檢查圖片是否上傳
21
+ if image is None:
22
+ return "錯誤:請上傳一張圖片"
23
+
24
+ # 檢查 prompt 是否為空
25
+ if not prompt or prompt.strip() == "":
26
+ return "錯誤:請輸入分析提示文字"
27
+
28
+ # 初始化 Groq 客戶端
29
+ client = Groq(api_key=api_key.strip())
30
+
31
+ # 編碼圖片
32
+ base64_image = encode_image(image)
33
+ image_content = {
34
+ "type": "image_url",
35
+ "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}
36
+ }
37
+
38
+ # 發送請求到 Groq API
39
+ completion = client.chat.completions.create(
40
+ model="meta-llama/llama-4-scout-17b-16e-instruct",
41
+ messages=[{
42
+ "role": "user",
43
+ "content": [
44
+ {
45
+ "type": "text",
46
+ "text": prompt
47
+ },
48
+ image_content
49
+ ]
50
+ }],
51
+ temperature=temperature,
52
+ max_completion_tokens=max_tokens,
53
+ top_p=1,
54
+ stream=False,
55
+ stop=None,
56
+ )
57
+
58
+ # 返回分析結果
59
+ return completion.choices[0].message.content
60
+
61
+ except Exception as e:
62
+ return f"錯誤:{str(e)}"
63
+
64
+ # 創建 Gradio 界面
65
+ with gr.Blocks(title="Groq 圖片分析器", theme=gr.themes.Soft()) as demo:
66
+ gr.Markdown(
67
+ """
68
+ # 🔍 Groq 圖片分析器
69
+
70
+ 使用 Groq API 和 Llama-4-Scout 模型來分析圖片內容
71
+
72
+ **使用說明:**
73
+ 1. 輸入您的 Groq API Key
74
+ 2. 上傳要分析的圖片
75
+ 3. 輸入分析提示文字
76
+ 4. 調整參數(可選)
77
+ 5. 點擊「分析圖片」按鈕
78
+ """
79
+ )
80
+
81
+ with gr.Row():
82
+ with gr.Column(scale=1):
83
+ # API Key 輸入
84
+ api_key = gr.Textbox(
85
+ label="Groq API Key",
86
+ placeholder="輸入您的 Groq API Key (例: gsk_...)",
87
+ type="password",
88
+ info="您的 API Key 將被安全處理,不會被儲存"
89
+ )
90
+
91
+ # 圖片上傳
92
+ image = gr.Image(
93
+ label="上傳圖片",
94
+ type="pil",
95
+ format="jpeg"
96
+ )
97
+
98
+ # Prompt 輸入
99
+ prompt = gr.Textbox(
100
+ label="分析提示",
101
+ placeholder="請描述您想要分析的內容...",
102
+ lines=5,
103
+ value="""幫我算出有幾個人和大象,同時說明
104
+ 這可能是什麼儀式?
105
+ 天氣和季節在某個時段?
106
+ 在什麼國家?"""
107
+ )
108
+
109
+ # 進階參數
110
+ with gr.Accordion("進階設定", open=False):
111
+ temperature = gr.Slider(
112
+ label="Temperature (創意度)",
113
+ minimum=0.1,
114
+ maximum=2.0,
115
+ value=1.0,
116
+ step=0.1,
117
+ info="數值越高,回答越有創意但可能不太準確"
118
+ )
119
+
120
+ max_tokens = gr.Slider(
121
+ label="最大回應長度",
122
+ minimum=100,
123
+ maximum=2048,
124
+ value=512,
125
+ step=50,
126
+ info="限制回應的最大字數"
127
+ )
128
+
129
+ # 分析按鈕
130
+ analyze_btn = gr.Button(
131
+ "🔍 分析圖片",
132
+ variant="primary",
133
+ size="lg"
134
+ )
135
+
136
+ with gr.Column(scale=1):
137
+ # 分析結果輸出
138
+ output = gr.Textbox(
139
+ label="分析結果",
140
+ lines=20,
141
+ max_lines=30,
142
+ show_copy_button=True,
143
+ placeholder="分析結果將顯示在這裡..."
144
+ )
145
+
146
+ # 綁定事件
147
+ analyze_btn.click(
148
+ fn=analyze_image,
149
+ inputs=[api_key, image, prompt, temperature, max_tokens],
150
+ outputs=output
151
+ )
152
+
153
+ # 示例區域
154
+ gr.Markdown(
155
+ """
156
+ ## 💡 提示範例
157
+
158
+ **分析人物和動物:**
159
+ - "請計算圖片中有多少人和動物,並描述他們在做什麼"
160
+
161
+ **場景描述:**
162
+ - "描述這張圖片的場景,包括時間、地點、天氣狀況"
163
+
164
+ **文化分析:**
165
+ - "這看起來像什麼文化��動或儀式?可能在哪個國家或地區?"
166
+
167
+ **物體識別:**
168
+ - "識別圖片中的所有物體並說明它們的用途"
169
+ """
170
+ )
171
+
172
+ # 啟動應用
173
+ if __name__ == "__main__":
174
+ demo.launch(
175
+ server_name="0.0.0.0",
176
+ server_port=7860,
177
+ share=True,
178
+ debug=False
179
+ )