ChenyuRabbitLove commited on
Commit
e1f9684
1 Parent(s): f825612

bugfix: fix minor bugs

Browse files
Files changed (2) hide show
  1. requirements.txt +1 -1
  2. utils/completion_reward.py +13 -35
requirements.txt CHANGED
@@ -7,4 +7,4 @@ google-cloud-storage
7
  google-cloud-bigquery
8
  openai
9
  opencc
10
- gradio==4.13.0
 
7
  google-cloud-bigquery
8
  openai
9
  opencc
10
+ google-cloud-aiplatform
utils/completion_reward.py CHANGED
@@ -21,11 +21,17 @@ from google.oauth2.service_account import Credentials
21
  from google import auth
22
  from google.cloud import bigquery
23
  from google.cloud import storage
 
 
24
 
25
  SERVICE_ACCOUNT_INFO = os.getenv("GBQ_TOKEN")
26
  SCOPES = ["https://www.googleapis.com/auth/cloud-platform"]
27
  service_account_info_dict = json.loads(SERVICE_ACCOUNT_INFO)
28
 
 
 
 
 
29
  creds = Credentials.from_service_account_info(service_account_info_dict, scopes=SCOPES)
30
 
31
  gbq_client = bigquery.Client(
@@ -35,7 +41,6 @@ gcs_client = storage.Client(
35
  credentials=creds, project=service_account_info_dict["project_id"]
36
  )
37
 
38
-
39
  class CompletionReward:
40
  def __init__(self):
41
  self.player_backend_user_id = None
@@ -262,6 +267,7 @@ class OpenAIAgent:
262
  retry_attempts = 0
263
  while retry_attempts < 5:
264
  try:
 
265
  response = client.images.generate(
266
  model="dall-e-3",
267
  prompt="Create an image in a retro Ghibli style, with a focus on a universe theme. The artwork should maintain the traditional hand-drawn animation look characteristic of Ghibli. Imagine a scene set in outer space or a fantastical cosmic environment, rich with vibrant and varied color palettes to capture the mystery and majesty of the universe. The background should be detailed, showcasing stars, planets, and nebulae, blending the Ghibli style's nostalgia and emotional depth with the awe-inspiring aspects of space. The overall feel should be timeless, merging the natural wonder of the cosmos with the storytelling and emotional resonance typical of the retro Ghibli aesthetic. Soft lighting and gentle shading should be used to enhance the dreamlike, otherworldly quality of the scene.",
@@ -275,7 +281,7 @@ class OpenAIAgent:
275
 
276
  except Exception as e:
277
  retry_attempts += 1
278
- logging.error(f"Attempt {retry_attempts}: {e}")
279
  time.sleep(1 * retry_attempts) # exponential backoff
280
 
281
 
@@ -319,15 +325,6 @@ class AWSAgent:
319
 
320
  class GoogleAgent:
321
  def get_story(self, user_log):
322
- creds.refresh(Request())
323
-
324
- url = f"https://us-central1-aiplatform.googleapis.com/v1/projects/junyiacademy/locations/us-central1/publishers/google/models/gemini-pro:streamGenerateContent?"
325
-
326
- # Headers
327
- headers = {
328
- "Authorization": f"Bearer {creds.token}",
329
- "Content-Type": "application/json",
330
- }
331
  system_prompt = """
332
  我正在舉辦一個學習型的活動,參與活動的學生為 1-9 年級,我為他們設計了一個獨特的故事機制,每天每個學生都會收到屬於自己獨特的冒險紀錄,現在我需要你協助我將這些冒險紀錄,製作成一段冒險故事,請
333
  - 以「你」稱呼學生
@@ -342,36 +339,17 @@ class GoogleAgent:
342
  ```
343
  """
344
 
345
- # Provided payload
346
- payload = {
347
- "contents": [
348
- {
349
- "role": "USER",
350
- "parts": {"text": f"{system_prompt}, 以下是我的冒險故事 ```{user_log}```"},
351
- },
352
- ],
353
- "safety_settings": {
354
- "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
355
- "threshold": "BLOCK_LOW_AND_ABOVE",
356
- },
357
- "generation_config": {
358
- "temperature": 0.2,
359
- "topP": 0.8,
360
- "topK": 40,
361
- "maxOutputTokens": 2048,
362
- },
363
- }
364
 
365
  retry_attempts = 0
366
  while retry_attempts < 5:
367
  try:
368
- response = requests.post(url, headers=headers, data=json.dumps(payload))
 
 
369
  chinese_converter = OpenCC("s2tw")
370
 
371
- final_story = ""
372
- for i in response.json():
373
- final_story += i["candidates"][0]["content"]["parts"][0]["text"]
374
- return chinese_converter.convert(final_story)
375
 
376
  except Exception as e:
377
  retry_attempts += 1
 
21
  from google import auth
22
  from google.cloud import bigquery
23
  from google.cloud import storage
24
+ from google.cloud import aiplatform
25
+ from vertexai.preview.generative_models import GenerativeModel
26
 
27
  SERVICE_ACCOUNT_INFO = os.getenv("GBQ_TOKEN")
28
  SCOPES = ["https://www.googleapis.com/auth/cloud-platform"]
29
  service_account_info_dict = json.loads(SERVICE_ACCOUNT_INFO)
30
 
31
+ aiplatform.init(
32
+ project='junyiacademy',
33
+ service_account=service_account_info_dict,
34
+ )
35
  creds = Credentials.from_service_account_info(service_account_info_dict, scopes=SCOPES)
36
 
37
  gbq_client = bigquery.Client(
 
41
  credentials=creds, project=service_account_info_dict["project_id"]
42
  )
43
 
 
44
  class CompletionReward:
45
  def __init__(self):
46
  self.player_backend_user_id = None
 
267
  retry_attempts = 0
268
  while retry_attempts < 5:
269
  try:
270
+ logging.info("Generating image...")
271
  response = client.images.generate(
272
  model="dall-e-3",
273
  prompt="Create an image in a retro Ghibli style, with a focus on a universe theme. The artwork should maintain the traditional hand-drawn animation look characteristic of Ghibli. Imagine a scene set in outer space or a fantastical cosmic environment, rich with vibrant and varied color palettes to capture the mystery and majesty of the universe. The background should be detailed, showcasing stars, planets, and nebulae, blending the Ghibli style's nostalgia and emotional depth with the awe-inspiring aspects of space. The overall feel should be timeless, merging the natural wonder of the cosmos with the storytelling and emotional resonance typical of the retro Ghibli aesthetic. Soft lighting and gentle shading should be used to enhance the dreamlike, otherworldly quality of the scene.",
 
281
 
282
  except Exception as e:
283
  retry_attempts += 1
284
+ logging.error(f"DALLE Attempt {retry_attempts}: {e}")
285
  time.sleep(1 * retry_attempts) # exponential backoff
286
 
287
 
 
325
 
326
  class GoogleAgent:
327
  def get_story(self, user_log):
 
 
 
 
 
 
 
 
 
328
  system_prompt = """
329
  我正在舉辦一個學習型的活動,參與活動的學生為 1-9 年級,我為他們設計了一個獨特的故事機制,每天每個學生都會收到屬於自己獨特的冒險紀錄,現在我需要你協助我將這些冒險紀錄,製作成一段冒險故事,請
330
  - 以「你」稱呼學生
 
339
  ```
340
  """
341
 
342
+ gemini_pro_model = GenerativeModel("gemini-pro")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
343
 
344
  retry_attempts = 0
345
  while retry_attempts < 5:
346
  try:
347
+ logging.info("Google Generating response...")
348
+ model_response = gemini_pro_model.generate_content(f"{system_prompt}, 以下是我的冒險故事 ```{user_log}```")
349
+
350
  chinese_converter = OpenCC("s2tw")
351
 
352
+ return chinese_converter.convert(model_response.candidates[0].content.parts[0].text)
 
 
 
353
 
354
  except Exception as e:
355
  retry_attempts += 1