Spaces:
Sleeping
Sleeping
data pop
Browse files
app.py
CHANGED
|
@@ -15,6 +15,7 @@ from moviepy.editor import VideoFileClip
|
|
| 15 |
from pytube import YouTube
|
| 16 |
import os
|
| 17 |
|
|
|
|
| 18 |
from google.oauth2 import service_account
|
| 19 |
from googleapiclient.discovery import build
|
| 20 |
from googleapiclient.http import MediaFileUpload
|
|
@@ -55,11 +56,35 @@ VIDEO_ID = ""
|
|
| 55 |
|
| 56 |
OPEN_AI_KEY = os.getenv("OPEN_AI_KEY")
|
| 57 |
client = OpenAI(api_key=OPEN_AI_KEY)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
|
| 60 |
# # ====drive====初始化Google Drive服务
|
| 61 |
def init_drive_service():
|
| 62 |
-
credentials_json_string =
|
| 63 |
credentials_dict = json.loads(credentials_json_string)
|
| 64 |
SCOPES = ['https://www.googleapis.com/auth/drive']
|
| 65 |
credentials = service_account.Credentials.from_service_account_info(
|
|
@@ -694,10 +719,15 @@ def respond(user_message, df_string_output, chat_history, socratic_mode=False):
|
|
| 694 |
print("=== 變數:chat_history ===")
|
| 695 |
print(chat_history)
|
| 696 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 697 |
if socratic_mode:
|
| 698 |
sys_content = f"""
|
| 699 |
你是一個擅長資料分析跟影片教學的老師,user 為學生
|
| 700 |
-
請用 {
|
| 701 |
並進行對話,使用 zh-TW
|
| 702 |
|
| 703 |
如果是影片類型,不用解釋逐字稿格式,直接回答學生問題
|
|
@@ -715,7 +745,7 @@ def respond(user_message, df_string_output, chat_history, socratic_mode=False):
|
|
| 715 |
else:
|
| 716 |
sys_content = f"""
|
| 717 |
你是一個擅長資料分析跟影片教學的老師,user 為學生
|
| 718 |
-
請用 {
|
| 719 |
並進行對話,使用 zh-TW
|
| 720 |
|
| 721 |
如果是影片類型,不用解釋逐字稿格式,直接回答學生問題
|
|
|
|
| 15 |
from pytube import YouTube
|
| 16 |
import os
|
| 17 |
|
| 18 |
+
from google.cloud import storage
|
| 19 |
from google.oauth2 import service_account
|
| 20 |
from googleapiclient.discovery import build
|
| 21 |
from googleapiclient.http import MediaFileUpload
|
|
|
|
| 56 |
|
| 57 |
OPEN_AI_KEY = os.getenv("OPEN_AI_KEY")
|
| 58 |
client = OpenAI(api_key=OPEN_AI_KEY)
|
| 59 |
+
DRIVE_KEY = os.getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON")
|
| 60 |
+
GCS_KEY = os.getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON")
|
| 61 |
+
|
| 62 |
+
def init_gcs_client(service_account_key_string):
|
| 63 |
+
"""使用服务账号密钥文件创建 GCS 客户端"""
|
| 64 |
+
credentials_json_string = service_account_key_string
|
| 65 |
+
credentials_dict = json.loads(credentials_json_string)
|
| 66 |
+
credentials = service_account.Credentials.from_service_account_info(credentials_dict)
|
| 67 |
+
gcs_client = storage.Client(credentials=credentials, project=credentials_dict['project_id'])
|
| 68 |
+
return gcs_client
|
| 69 |
+
|
| 70 |
+
def upload_file_to_gcs(gcs_client, bucket_name, source_file_name, destination_blob_name, service_account_key_file):
|
| 71 |
+
"""上传文件到 GCS"""
|
| 72 |
+
bucket = gcs_client.bucket(bucket_name)
|
| 73 |
+
blob = bucket.blob(destination_blob_name)
|
| 74 |
+
blob.upload_from_filename(source_file_name)
|
| 75 |
+
print(f"{source_file_name} uploaded to {destination_blob_name}.")
|
| 76 |
+
|
| 77 |
+
def download_file_from_gcs(gcs_client, bucket_name, source_blob_name, destination_file_name, service_account_key_file):
|
| 78 |
+
"""从 GCS 下载文件"""
|
| 79 |
+
bucket = gcs_client.bucket(bucket_name)
|
| 80 |
+
blob = bucket.blob(source_blob_name)
|
| 81 |
+
blob.download_to_filename(destination_file_name)
|
| 82 |
+
print(f"{source_blob_name} downloaded to {destination_file_name}.")
|
| 83 |
|
| 84 |
|
| 85 |
# # ====drive====初始化Google Drive服务
|
| 86 |
def init_drive_service():
|
| 87 |
+
credentials_json_string = DRIVE_KEY
|
| 88 |
credentials_dict = json.loads(credentials_json_string)
|
| 89 |
SCOPES = ['https://www.googleapis.com/auth/drive']
|
| 90 |
credentials = service_account.Credentials.from_service_account_info(
|
|
|
|
| 719 |
print("=== 變數:chat_history ===")
|
| 720 |
print(chat_history)
|
| 721 |
|
| 722 |
+
data = df_string_output
|
| 723 |
+
for entry in data:
|
| 724 |
+
entry.pop('embed_url', None) # Remove 'embed_url' if it exists
|
| 725 |
+
entry.pop('screenshot_path', None)
|
| 726 |
+
|
| 727 |
if socratic_mode:
|
| 728 |
sys_content = f"""
|
| 729 |
你是一個擅長資料分析跟影片教學的老師,user 為學生
|
| 730 |
+
請用 {data} 為資料文本,自行判斷資料的種類,
|
| 731 |
並進行對話,使用 zh-TW
|
| 732 |
|
| 733 |
如果是影片類型,不用解釋逐字稿格式,直接回答學生問題
|
|
|
|
| 745 |
else:
|
| 746 |
sys_content = f"""
|
| 747 |
你是一個擅長資料分析跟影片教學的老師,user 為學生
|
| 748 |
+
請用 {data} 為資料文本,自行判斷資料的種類,
|
| 749 |
並進行對話,使用 zh-TW
|
| 750 |
|
| 751 |
如果是影片類型,不用解釋逐字稿格式,直接回答學生問題
|