SamiKoen commited on
Commit
bf7f1a9
·
verified ·
1 Parent(s): d5d47bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -11
app.py CHANGED
@@ -3,13 +3,17 @@ import os
3
  import json
4
  import requests
5
  import xml.etree.ElementTree as ET
6
- from pydrive.auth import GoogleAuth
7
- from pydrive.drive import GoogleDrive
 
8
 
9
- # Google Drive ile kimlik doğrulaması yapma
10
- gauth = GoogleAuth()
11
- gauth.LocalWebserverAuth()
12
- drive = GoogleDrive(gauth)
 
 
 
13
 
14
  def save_to_drive(chat_history):
15
  # Sohbet geçmişini bir dosyaya kaydetme
@@ -17,11 +21,11 @@ def save_to_drive(chat_history):
17
  for message in chat_history:
18
  f.write(f"{message['role']}: {message['content']}\n")
19
 
20
- # Dosyayı Google Drive'a yükleme
21
- file = drive.CreateFile({'title': 'chat_history.txt'})
22
- file.SetContentFile('chat_history.txt')
23
- file.Upload()
24
- return "Sohbet geçmişi Google Drive'a yüklendi."
25
 
26
  # os.getenv("API_URL") + "/generate_stream"
27
  API_URL = "https://api.openai.com/v1/chat/completions"
 
3
  import json
4
  import requests
5
  import xml.etree.ElementTree as ET
6
+ from google.oauth2 import service_account
7
+ from googleapiclient.discovery import build
8
+ from googleapiclient.http import MediaFileUpload
9
 
10
+ # Google Drive API ile kimlik doğrulaması yapma
11
+ SERVICE_ACCOUNT_FILE = 'path/to/your-service-account-file.json'
12
+ SCOPES = ['https://www.googleapis.com/auth/drive.file']
13
+
14
+ credentials = service_account.Credentials.from_service_account_file(
15
+ SERVICE_ACCOUNT_FILE, scopes=SCOPES)
16
+ service = build('drive', 'v3', credentials=credentials)
17
 
18
  def save_to_drive(chat_history):
19
  # Sohbet geçmişini bir dosyaya kaydetme
 
21
  for message in chat_history:
22
  f.write(f"{message['role']}: {message['content']}\n")
23
 
24
+ file_metadata = {'name': 'chat_history.txt'}
25
+ media = MediaFileUpload('chat_history.txt', mimetype='text/plain')
26
+ file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
27
+
28
+ return f"Sohbet geçmişi Google Drive'a yüklendi. Dosya ID: {file.get('id')}"
29
 
30
  # os.getenv("API_URL") + "/generate_stream"
31
  API_URL = "https://api.openai.com/v1/chat/completions"