cjian2025 commited on
Commit
4146af6
·
verified ·
1 Parent(s): 1ee2b32

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +178 -0
app.py ADDED
@@ -0,0 +1,178 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from groq import Groq
3
+ from twilio.rest import Client
4
+ import base64
5
+ import io
6
+ from PIL import Image
7
+
8
+ # Groq API 設定
9
+ GROQ_API_KEY = "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"
10
+
11
+ # Twilio 設定
12
+ TWILIO_ACCOUNT_SID = "ACd7f840e0ea441d49885b4ab7a8828c46"
13
+ TWILIO_AUTH_TOKEN = "b3f7af530024eb7bf68e3576f5fe3ad7"
14
+ TWILIO_FROM_NUMBER = "+17623355728"
15
+
16
+ def encode_image(image):
17
+ """將 PIL Image 轉換為 base64 編碼"""
18
+ buffered = io.BytesIO()
19
+ image.save(buffered, format="JPEG")
20
+ return base64.b64encode(buffered.getvalue()).decode("utf-8")
21
+
22
+ def analyze_image(image):
23
+ """使用 Groq API 分析圖片"""
24
+ try:
25
+ # 初始化 Groq 客戶端
26
+ client = Groq(api_key=GROQ_API_KEY)
27
+
28
+ # 編碼圖片
29
+ base64_image = encode_image(image)
30
+ image_content = {
31
+ "type": "image_url",
32
+ "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}
33
+ }
34
+
35
+ # 發送請求到 Groq
36
+ completion = client.chat.completions.create(
37
+ model="meta-llama/llama-4-scout-17b-16e-instruct",
38
+ messages=[{
39
+ "role": "user",
40
+ "content": [
41
+ {
42
+ "type": "text",
43
+ "text": "解釋圖片的內容,10個字說明,使用繁體中文。"
44
+ },
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
+ return completion.choices[0].message.content
56
+
57
+ except Exception as e:
58
+ return f"圖片分析失敗:{str(e)}"
59
+
60
+ def send_sms(phone_number, message_body):
61
+ """使用 Twilio 發送 SMS"""
62
+ try:
63
+ # 初始化 Twilio 客戶端
64
+ client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
65
+
66
+ # 發送訊息
67
+ message = client.messages.create(
68
+ from_=TWILIO_FROM_NUMBER,
69
+ to=phone_number,
70
+ body=message_body
71
+ )
72
+
73
+ return f"訊息發送成功!訊息 ID: {message.sid}"
74
+
75
+ except Exception as e:
76
+ return f"訊息發送失敗:{str(e)}"
77
+
78
+ def process_image_and_sms(image, phone_number, custom_message):
79
+ """主要處理函數:分析圖片並發送 SMS"""
80
+ if image is None:
81
+ return "請上傳圖片", ""
82
+
83
+ if not phone_number:
84
+ return "請輸入手機號碼", ""
85
+
86
+ # 分析圖片
87
+ analysis_result = analyze_image(image)
88
+
89
+ # 準備要發送的訊息
90
+ if custom_message:
91
+ # 如果有自訂訊息,使用自訂訊息
92
+ sms_body = custom_message
93
+ else:
94
+ # 如果沒有自訂訊息,使用圖片分析結果
95
+ sms_body = f"圖片分析結果:{analysis_result}"
96
+
97
+ # 發送 SMS
98
+ sms_result = send_sms(phone_number, sms_body)
99
+
100
+ return analysis_result, sms_result
101
+
102
+ # 創建 Gradio 介面
103
+ with gr.Blocks(title="圖片分析與SMS發送應用") as demo:
104
+ gr.Markdown("# 圖片分析與SMS發送應用")
105
+ gr.Markdown("上傳圖片進行AI分析,並可選擇發送結果或自訂訊息到指定手機號碼")
106
+
107
+ with gr.Row():
108
+ with gr.Column():
109
+ # 輸入區域
110
+ image_input = gr.Image(
111
+ type="pil",
112
+ label="上傳圖片"
113
+ )
114
+ phone_input = gr.Textbox(
115
+ label="手機號碼",
116
+ placeholder="例如:+886919017732",
117
+ info="請輸入完整的國際格式手機號碼"
118
+ )
119
+ message_input = gr.Textbox(
120
+ label="自訂訊息(選填)",
121
+ placeholder="如果留空,將發送圖片分析結果",
122
+ lines=3
123
+ )
124
+
125
+ # 按鈕
126
+ analyze_btn = gr.Button("分析圖片", variant="secondary")
127
+ send_btn = gr.Button("分析並發送SMS", variant="primary")
128
+
129
+ with gr.Column():
130
+ # 輸出區域
131
+ analysis_output = gr.Textbox(
132
+ label="圖片分析結果",
133
+ lines=4,
134
+ interactive=False
135
+ )
136
+ sms_output = gr.Textbox(
137
+ label="SMS發送狀態",
138
+ lines=2,
139
+ interactive=False
140
+ )
141
+
142
+ # 只分析圖片(不發送SMS)
143
+ analyze_btn.click(
144
+ fn=lambda img: analyze_image(img) if img else "請上傳圖片",
145
+ inputs=[image_input],
146
+ outputs=[analysis_output]
147
+ )
148
+
149
+ # 分析圖片並發送SMS
150
+ send_btn.click(
151
+ fn=process_image_and_sms,
152
+ inputs=[image_input, phone_input, message_input],
153
+ outputs=[analysis_output, sms_output]
154
+ )
155
+
156
+ # 使用範例
157
+ gr.Markdown("""
158
+ ## 使用說明:
159
+ 1. **上傳圖片**:點擊圖片區域上傳要分析的圖片
160
+ 2. **輸入手機號碼**:輸入接收SMS的手機號碼(國際格式,如 +886919017732)
161
+ 3. **選擇操作**:
162
+ - 點擊「分析圖片」只進行圖片分析
163
+ - 點擊「分析並發送SMS」會分析圖片並發送結果到指定手機
164
+ 4. **自訂訊息**:如果想發送自訂內容而非圖片分析結果,可在自訂訊息欄位輸入
165
+
166
+ ## 注意事項:
167
+ - 請確保手機號碼格式正確(包含國碼)
168
+ - 圖片分析使用 Groq 的 Llama 模型
169
+ - SMS 發送使用 Twilio 服務
170
+ """)
171
+
172
+ # 啟動應用
173
+ if __name__ == "__main__":
174
+ demo.launch(
175
+ share=True, # 設為 True 可獲得公開連結
176
+ server_name="0.0.0.0", # 允許外部連接
177
+ server_port=7860 # 指定端口
178
+ )