groq_API_gradio / app.py
z9760405's picture
Create app.py
e94939b verified
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
)