Roberta2024 commited on
Commit
dfcff84
·
verified ·
1 Parent(s): 895c5be

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +145 -0
app.py ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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圖片轉換為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):
14
+ """使用Groq API分析圖片"""
15
+ try:
16
+ # 檢查輸入
17
+ if not api_key:
18
+ return "錯誤:請輸入 API Key"
19
+
20
+ if image is None:
21
+ return "錯誤:請上傳圖片"
22
+
23
+ if not prompt.strip():
24
+ return "錯誤:請輸入提示詞"
25
+
26
+ # 初始化 Groq 客戶端
27
+ client = Groq(api_key=api_key)
28
+
29
+ # 將圖片轉換為base64
30
+ base64_image = encode_image(image)
31
+
32
+ # 建立圖片內容
33
+ image_content = {
34
+ "type": "image_url",
35
+ "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}
36
+ }
37
+
38
+ # 呼叫 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
+ {"type": "text", "text": prompt},
45
+ image_content
46
+ ]
47
+ }],
48
+ temperature=1,
49
+ max_completion_tokens=512,
50
+ top_p=1,
51
+ stream=False,
52
+ stop=None,
53
+ )
54
+
55
+ # 回傳結果
56
+ return completion.choices[0].message.content
57
+
58
+ except Exception as e:
59
+ return f"發生錯誤:{str(e)}"
60
+
61
+ # 建立 Gradio 介面
62
+ def create_interface():
63
+ with gr.Blocks(title="Multidata_API_圖片分析器", theme=gr.themes.Soft()) as iface:
64
+ gr.Markdown("# 🖼️ Groq 圖片分析器")
65
+ gr.Markdown("使用 Groq API 和 Llama 模型來分析和描述圖片內容")
66
+
67
+ with gr.Row():
68
+ with gr.Column(scale=1):
69
+ # 輸入區域
70
+ api_key_input = gr.Textbox(
71
+ label="🔑 Groq API Key",
72
+ placeholder="請輸入您的 Groq API Key",
73
+ type="password"
74
+ )
75
+
76
+ image_input = gr.Image(
77
+ label="📸 上傳圖片",
78
+ type="pil",
79
+ sources=["upload", "clipboard"]
80
+ )
81
+
82
+ prompt_input = gr.Textbox(
83
+ label="💭 提示詞",
84
+ placeholder="請輸入您想要的分析要求,例如:幫我說明這張圖片,使用繁體中文",
85
+ lines=3,
86
+ value="幫我說明這張圖片,使用繁體中文"
87
+ )
88
+
89
+ analyze_btn = gr.Button("🔍 分析圖片", variant="primary")
90
+
91
+ with gr.Column(scale=1):
92
+ # 輸出區域
93
+ output_text = gr.Textbox(
94
+ label="📝 分析結果",
95
+ lines=15,
96
+ max_lines=20,
97
+ interactive=False
98
+ )
99
+
100
+ # 範例區域
101
+ with gr.Accordion("💡 使用說明", open=False):
102
+ gr.Markdown("""
103
+ ### 如何使用:
104
+ 1. **輸入 API Key**:在上方輸入您的 Groq API Key
105
+ 2. **上傳圖片**:點擊圖片區域上傳您想分析的圖片
106
+ 3. **輸入提示詞**:描述您希望 AI 如何分析這張圖片
107
+ 4. **點擊分析**:點擊「分析圖片」按鈕開始分析
108
+
109
+ ### 提示詞範例:
110
+ - `幫我說明這張圖片,使用繁體中文`
111
+ - `描述這張圖片中的人物、場景和活動`
112
+ - `分析這張圖片的構圖和色彩運用`
113
+ - `這張圖片可能在哪裡拍攝的?`
114
+
115
+ ### 注意事項:
116
+ - 請確保您的 Groq API Key 有效且有足夠額度
117
+ - 支援的圖片格式:JPG, PNG, WEBP 等常見格式
118
+ - 圖片大小建議不超過 10MB
119
+ """)
120
+
121
+ # 綁定按鈕事件
122
+ analyze_btn.click(
123
+ fn=analyze_image,
124
+ inputs=[api_key_input, image_input, prompt_input],
125
+ outputs=output_text
126
+ )
127
+
128
+ # 也可以按 Enter 鍵觸發分析
129
+ prompt_input.submit(
130
+ fn=analyze_image,
131
+ inputs=[api_key_input, image_input, prompt_input],
132
+ outputs=output_text
133
+ )
134
+
135
+ return iface
136
+
137
+ # 啟動應用程式
138
+ if __name__ == "__main__":
139
+ app = create_interface()
140
+ app.launch(
141
+ server_name="0.0.0.0", # 允許外部訪問
142
+ server_port=7860, # 指定端口
143
+ share=False, # 設為 True 可獲得公開連結
144
+ debug=True # 開啟偵錯模式
145
+ )