Spaces:
Sleeping
Sleeping
File size: 6,358 Bytes
3063936 ec593e6 3063936 ec593e6 3063936 ec593e6 3063936 ec593e6 11f0666 ec593e6 3063936 ec593e6 3063936 ec593e6 11f0666 3063936 ec593e6 3063936 ec593e6 11f0666 ec593e6 11f0666 ec593e6 3063936 ec593e6 3063936 ec593e6 11f0666 3063936 ec593e6 11f0666 ec593e6 3063936 ec593e6 11f0666 ec593e6 3063936 ec593e6 3063936 11f0666 3063936 ec593e6 3063936 |
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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
import gradio as gr
from groq import Groq
from twilio.rest import Client
import base64
import io
from PIL import Image
def encode_image(image):
"""將PIL圖片編碼為base64字符串"""
buffered = io.BytesIO()
image.save(buffered, format="JPEG")
return base64.b64encode(buffered.getvalue()).decode("utf-8")
def analyze_and_send_sms(
image,
groq_api_key,
account_sid,
auth_token,
from_number,
to_number,
custom_message=""
):
"""
分析圖片並發送SMS訊息
Args:
image: 上傳的圖片
groq_api_key: Groq API金鑰
account_sid: Twilio帳戶SID
auth_token: Twilio認證令牌
from_number: Twilio虛擬手機號碼
to_number: 接收SMS的手機號碼
custom_message: 自定義訊息內容
Returns:
tuple: (圖片分析結果, SMS發送狀態)
"""
try:
# 步驟1: 使用Groq分析圖片
if image is None:
return "錯誤:請上傳圖片", "未發送SMS"
if not groq_api_key:
return "錯誤:請輸入Groq API金鑰", "未發送SMS"
# 編碼圖片
base64_image = encode_image(image)
image_content = {
"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}
}
# 設定提示詞
prompt = "解釋圖片的內容,10個字說明,使用繁體中文。"
# 建立Groq客戶端
groq_client = Groq(api_key=groq_api_key)
# 呼叫Groq API
completion = groq_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,
)
# 取得圖片分析結果
analysis_result = completion.choices[0].message.content
# 步驟2: 發送SMS
if not all([account_sid, auth_token, from_number, to_number]):
return analysis_result, "錯誤:請填入所有Twilio設定"
# 準備SMS內容
if custom_message:
sms_body = custom_message
else:
sms_body = f"圖片分析結果:{analysis_result}"
# 建立Twilio客戶端
twilio_client = Client(account_sid, auth_token)
# 發送SMS
message = twilio_client.messages.create(
from_=from_number,
to=to_number,
body=sms_body
)
sms_status = f"SMS發送成功!訊息SID: {message.sid}"
return analysis_result, sms_status
except Exception as e:
error_msg = f"發生錯誤: {str(e)}"
return error_msg, "SMS發送失敗"
# 建立Gradio介面
with gr.Blocks(title="圖片分析與SMS發送應用") as demo:
gr.Markdown("# 圖片分析與SMS發送應用")
gr.Markdown("上傳圖片進行AI分析,並可選擇發送分析結果到手機")
with gr.Row():
with gr.Column():
# 圖片上傳
image_input = gr.Image(
type="pil",
label="上傳圖片",
height=300
)
# Groq API設定
groq_key = gr.Textbox(
label="Groq API金鑰",
type="password",
placeholder="輸入您的Groq API金鑰"
)
with gr.Column():
# Twilio設定
account_sid = gr.Textbox(
label="Twilio Account SID",
placeholder="ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
)
auth_token = gr.Textbox(
label="Twilio Auth Token",
type="password",
placeholder="輸入您的Twilio認證令牌"
)
from_number = gr.Textbox(
label="Twilio虛擬手機號碼",
placeholder="+1xxxxxxxxxx"
)
to_number = gr.Textbox(
label="接收SMS的手機號碼",
placeholder="+886xxxxxxxxx"
)
custom_message = gr.Textbox(
label="自定義訊息內容 (選填)",
placeholder="留空則自動使用圖片分析結果",
lines=3
)
# 執行按鈕
analyze_button = gr.Button("分析圖片並發送SMS", variant="primary")
# 輸出結果
with gr.Row():
analysis_output = gr.Textbox(
label="圖片分析結果",
interactive=False
)
sms_output = gr.Textbox(
label="SMS發送狀態",
interactive=False
)
# 設定按鈕點擊事件
analyze_button.click(
fn=analyze_and_send_sms,
inputs=[
image_input,
groq_key,
account_sid,
auth_token,
from_number,
to_number,
custom_message
],
outputs=[analysis_output, sms_output]
)
# 使用範例
gr.Markdown("""
## 使用說明:
1. **上傳圖片**:選擇要分析的圖片檔案
2. **輸入Groq API金鑰**:從Groq官網取得您的API金鑰
3. **設定Twilio參數**:
- Account SID:您的Twilio帳戶識別碼
- Auth Token:您的Twilio認證令牌
- 虛擬手機號碼:Twilio提供的發送號碼
- 接收手機號碼:要接收SMS的號碼
4. **自定義訊息**(選填):如果留空,會自動發送圖片分析結果
5. **點擊執行**:系統會分析圖片並發送SMS
## 注意事項:
- 請確保Twilio帳戶有足夠餘額
- 手機號碼需包含國碼(如台灣:+886)
- API金鑰請妥善保管,勿洩露給他人
""")
# 啟動應用程式
if __name__ == "__main__":
demo.launch(
share=False, # 設為True可產生公開連結
debug=True,
# 移除固定端口設定,讓Gradio自動尋找可用端口
) |