youngtsai commited on
Commit
0db3316
·
1 Parent(s): 29186a4
Files changed (2) hide show
  1. app.py +58 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from google.cloud import storage
3
+ import gspread
4
+ from oauth2client.service_account import ServiceAccountCredentials
5
+ import openai
6
+ import os
7
+ import time
8
+
9
+ # 初始化API和認證
10
+ api_key = 'your_openai_api_key'
11
+ openai_client = openai.Client(api_key=api_key)
12
+ gcs_client = storage.Client.from_service_account_json('your_gcs_credentials.json')
13
+ bucket_name = 'your_gcs_bucket_name'
14
+ bucket = gcs_client.bucket(bucket_name)
15
+
16
+ scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
17
+ creds = ServiceAccountCredentials.from_json_keyfile_name('your_google_sheets_credentials.json', scope)
18
+ sheets_client = gspread.authorize(creds)
19
+
20
+ # 函数定义
21
+ def upload_image_to_gcs(image_file, bucket):
22
+ # 为了避免文件名冲突,使用时间戳创建一个唯一的文件名
23
+ unique_filename = f"{int(time.time())}_{image_file.name}"
24
+ blob = bucket.blob(unique_filename)
25
+ blob.upload_from_string(image_file.read(), content_type=image_file.type)
26
+ blob.make_public()
27
+ return blob.public_url
28
+
29
+ def process_image(image_url):
30
+ # 这里调用OpenAI API或其他图像处理逻辑
31
+ text = f"处理结果: {image_url}" # 示例文本,实际应用中应调用API获取结果
32
+ return text
33
+
34
+ def process_and_upload(images, sheet_url):
35
+ sheet = sheets_client.open_by_url(sheet_url).sheet1
36
+
37
+ for image_file in images:
38
+ image_url = upload_image_to_gcs(image_file, bucket)
39
+ text = process_image(image_url)
40
+ sheet.append_row([image_url, text])
41
+
42
+ return "处理完成,结果已上传到Google Sheets"
43
+
44
+ # Gradio界面
45
+ with gr.Blocks() as demo:
46
+ gr.Markdown("## 批量上传图片并处理")
47
+ with gr.Row():
48
+ image_input = gr.File(label="选择图片", type="file", multiple=True)
49
+ sheet_input = gr.Textbox(label="Google Sheets URL")
50
+ submit_button = gr.Button("开始处理")
51
+
52
+ submit_button.click(
53
+ fn=process_and_upload,
54
+ inputs=[image_input, sheet_input],
55
+ outputs=gr.Markdown()
56
+ )
57
+
58
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio
2
+ google-cloud-storage
3
+ gspread
4
+ oauth2client
5
+ openai