File size: 4,879 Bytes
bfb4863
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import gradio as gr
from groq import Groq
import base64
import io
from PIL import Image

def encode_image(image):
    """將圖片編碼為base64格式"""
    buffered = io.BytesIO()
    image.save(buffered, format="JPEG")
    return base64.b64encode(buffered.getvalue()).decode("utf-8")

def analyze_image(api_key, image, prompt):
    """分析圖片的主要函數"""
    try:
        # 檢查輸入
        if not api_key:
            return "請輸入有效的 Groq API Key"

        if image is None:
            return "請上傳一張圖片"

        if not prompt:
            return "請輸入分析提示詞"

        # 編碼圖片
        base64_image = encode_image(image)
        image_content = {
            "type": "image_url",
            "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}
        }

        # 初始化 Groq 客戶端
        client = Groq(api_key=api_key)

        # 發送請求
        completion = client.chat.completions.create(
            model="meta-llama/llama-4-scout-17b-16e-instruct",
            messages=[{
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": prompt
                    },
                    image_content
                ]
            }],
            temperature=1,
            max_completion_tokens=512,
            top_p=1,
            stream=False,
            stop=None,
        )

        # 返回回應內容
        return completion.choices[0].message.content

    except Exception as e:
        return f"發生錯誤:{str(e)}"

# 創建 Gradio 界面
with gr.Blocks(title="多模態API圖片分析工具", theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
    # 🔍 Groq 圖片分析工具

    使用 Groq 的 Llama 4 Scout 模型來分析圖片內容
    """)

    with gr.Row():
        with gr.Column(scale=1):
            # API Key 輸入
            api_key_input = gr.Textbox(
                label="Groq API Key",
                placeholder="輸入您的 Groq API Key (gsk_...)",
                type="password",
                info="您的 API Key 將保密處理"
            )

            # 圖片上傳
            image_input = gr.Image(
                label="上傳圖片",
                type="pil",
                sources=["upload", "clipboard"],
                height=300
            )

            # Prompt 輸入
            prompt_input = gr.Textbox(
                label="分析提示詞",
                placeholder="請輸入您想要分析的內容...",
                lines=4,
                value="""幫我算出有幾個人和大象,同時說明
這可能是什麼儀式?
天氣和季節在某個時段?
在什麼國家?"""
            )

            # 分析按鈕
            analyze_btn = gr.Button("🚀 開始分析", variant="primary", size="lg")

        with gr.Column(scale=1):
            # 結果顯示
            result_output = gr.Textbox(
                label="分析結果",
                lines=15,
                max_lines=20,
                show_copy_button=True,
                placeholder="分析結果將顯示在這裡..."
            )

    # 範例區域
    with gr.Row():
        gr.Examples(
            examples=[
                ["分析這張圖片中的物體和場景"],
                ["描述這張圖片的顏色、構圖和視覺元素"],
                ["識別圖片中的文字內容"],
                ["分析這張圖片的情感和氛圍"],
                ["統計圖片中的人數和動物數量"]
            ],
            inputs=[prompt_input],
            label="範例提示詞"
        )

    # 綁定事件
    analyze_btn.click(
        fn=analyze_image,
        inputs=[api_key_input, image_input, prompt_input],
        outputs=[result_output]
    )

    # 使用說明
    with gr.Accordion("📖 使用說明", open=False):
        gr.Markdown("""
        ### 如何使用:
        1. **取得 API Key**: 前往 [Groq Console](https://console.groq.com/) 註冊並取得 API Key
        2. **輸入 API Key**: 在上方欄位輸入您的 Groq API Key
        3. **上傳圖片**: 點擊上傳區域選擇圖片,或直接拖拽圖片到上傳區域
        4. **輸入提示詞**: 描述您想要分析的內容
        5. **開始分析**: 點擊「開始分析」按鈕

        ### 支援的圖片格式:
        - JPEG, PNG, GIF, WebP
        - 建議圖片大小不超過 10MB

        ### 注意事項:
        - API Key 僅在當前會話中使用,不會被保存
        - 請確保您的 Groq 帳戶有足夠的使用額度
        - 分析時間取決於圖片大小和網路狀況
        """)

# 啟動應用
if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False,
        debug=True
    )