Spaces:
Sleeping
Sleeping
gemini
Browse files- ai_model.py +23 -0
- app.py +57 -13
- requirements.txt +6 -0
ai_model.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import json
|
| 3 |
+
import time
|
| 4 |
+
import io
|
| 5 |
+
import os
|
| 6 |
+
import re
|
| 7 |
+
import requests
|
| 8 |
+
import random
|
| 9 |
+
import hashlib
|
| 10 |
+
from datetime import datetime
|
| 11 |
+
|
| 12 |
+
from openai import OpenAI
|
| 13 |
+
|
| 14 |
+
from google.auth.transport.requests import Request
|
| 15 |
+
from google.oauth2.service_account import Credentials
|
| 16 |
+
from google import auth
|
| 17 |
+
from google.cloud import bigquery
|
| 18 |
+
from google.cloud import storage
|
| 19 |
+
|
| 20 |
+
from google.cloud import aiplatform
|
| 21 |
+
from vertexai.preview.generative_models import GenerativeModel
|
| 22 |
+
from google.oauth2.service_account import Credentials
|
| 23 |
+
|
app.py
CHANGED
|
@@ -4,17 +4,39 @@ import pandas as pd
|
|
| 4 |
from openai import OpenAI
|
| 5 |
import json
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# 設置 OpenAI API 客戶端
|
| 8 |
IS_ENV_LOCAL = os.getenv("IS_ENV_LOCAL", False)
|
| 9 |
-
|
| 10 |
if IS_ENV_LOCAL:
|
| 11 |
local_json = json.load(open("local.json"))
|
| 12 |
openai_api_key = local_json["OPENAI_API_KEY"]
|
|
|
|
|
|
|
| 13 |
else:
|
| 14 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
|
|
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
openai_client = OpenAI(api_key=openai_api_key)
|
| 18 |
|
| 19 |
def extract_article_from_content(article_text):
|
| 20 |
start_markers = ["新文章:", "New Article:", "Here it is:"]
|
|
@@ -70,6 +92,8 @@ def generate_new_article(lesson_words, original_article, original_word_count, ba
|
|
| 70 |
print(f"Attempt {attempt} to generate new article")
|
| 71 |
print("===========================================")
|
| 72 |
|
|
|
|
|
|
|
| 73 |
prompt = f"""
|
| 74 |
Please write a new and original Chinese article tailored for first-grade students. Here's a summary of the key points that you should follow:
|
| 75 |
|
|
@@ -88,16 +112,23 @@ def generate_new_article(lesson_words, original_article, original_word_count, ba
|
|
| 88 |
"Original Article" for Reference: The example provided is {original_article}。This article serves as a model for the spirit, style, and rhythmic pattern to be emulated.
|
| 89 |
"""
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
generated_article = extract_article_from_content(generated_text)
|
| 102 |
|
| 103 |
validate_article_result = validate_article(generated_article, lesson_words, base_chars, original_word_count)
|
|
@@ -181,8 +212,8 @@ with gr.Blocks() as demo:
|
|
| 181 |
base_chars_input = gr.Textbox(label="Base Characters")
|
| 182 |
|
| 183 |
with gr.Row():
|
|
|
|
| 184 |
with gr.Column():
|
| 185 |
-
model_list = ["gpt-4-0125-preview", "gpt-3.5-turbo", "gpt-4"]
|
| 186 |
model_1 = gr.Dropdown(label="Model 1", choices=model_list, value="gpt-4-0125-preview")
|
| 187 |
generate_button1 = gr.Button("Generate Article - gpt-4-0125-preview")
|
| 188 |
with gr.Column():
|
|
@@ -206,6 +237,14 @@ with gr.Blocks() as demo:
|
|
| 206 |
with gr.Column():
|
| 207 |
# validate_article_result_3 Json format
|
| 208 |
validate_article_result_3 = gr.JSON()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
generate_button1.click(
|
| 211 |
generate_new_article,
|
|
@@ -222,6 +261,11 @@ with gr.Blocks() as demo:
|
|
| 222 |
inputs=[lesson_words_input, original_article_input, original_word_count_input, base_chars_input, model_3],
|
| 223 |
outputs=[output_text3, validate_article_result_3]
|
| 224 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
|
| 226 |
# 為其他模型添加點擊事件
|
| 227 |
lesson_csv_file_input.change(
|
|
|
|
| 4 |
from openai import OpenAI
|
| 5 |
import json
|
| 6 |
|
| 7 |
+
|
| 8 |
+
from google.cloud import aiplatform
|
| 9 |
+
from vertexai.preview.generative_models import GenerativeModel
|
| 10 |
+
from google.oauth2.service_account import Credentials
|
| 11 |
+
|
| 12 |
+
|
| 13 |
# 設置 OpenAI API 客戶端
|
| 14 |
IS_ENV_LOCAL = os.getenv("IS_ENV_LOCAL", False)
|
|
|
|
| 15 |
if IS_ENV_LOCAL:
|
| 16 |
local_json = json.load(open("local.json"))
|
| 17 |
openai_api_key = local_json["OPENAI_API_KEY"]
|
| 18 |
+
GOOGLE_SERVICE_ACCOUNT_INFO = local_json["GBQ_TOKEN"]
|
| 19 |
+
google_service_account_info_dict = GOOGLE_SERVICE_ACCOUNT_INFO
|
| 20 |
else:
|
| 21 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 22 |
+
GOOGLE_SERVICE_ACCOUNT_INFO = os.getenv("GBQ_TOKEN")
|
| 23 |
+
google_service_account_info_dict = json.loads(GOOGLE_SERVICE_ACCOUNT_INFO)
|
| 24 |
|
| 25 |
+
# OPENAI
|
| 26 |
+
OPENAI_CLIENT = OpenAI(api_key=openai_api_key)
|
| 27 |
+
|
| 28 |
+
# GOOGLE
|
| 29 |
+
GOOGPE_SCOPES = ["https://www.googleapis.com/auth/cloud-platform"]
|
| 30 |
+
google_creds = Credentials.from_service_account_info(
|
| 31 |
+
google_service_account_info_dict, scopes=GOOGPE_SCOPES
|
| 32 |
+
)
|
| 33 |
+
aiplatform.init(
|
| 34 |
+
project="junyiacademy",
|
| 35 |
+
service_account=google_service_account_info_dict,
|
| 36 |
+
credentials=google_creds,
|
| 37 |
+
)
|
| 38 |
+
GEMINI_MODEL = GenerativeModel("gemini-pro")
|
| 39 |
|
|
|
|
| 40 |
|
| 41 |
def extract_article_from_content(article_text):
|
| 42 |
start_markers = ["新文章:", "New Article:", "Here it is:"]
|
|
|
|
| 92 |
print(f"Attempt {attempt} to generate new article")
|
| 93 |
print("===========================================")
|
| 94 |
|
| 95 |
+
system_prompt = "You are a creative writer specialized in Chinese Children book. You will help me write Chinese Articles."
|
| 96 |
+
|
| 97 |
prompt = f"""
|
| 98 |
Please write a new and original Chinese article tailored for first-grade students. Here's a summary of the key points that you should follow:
|
| 99 |
|
|
|
|
| 112 |
"Original Article" for Reference: The example provided is {original_article}。This article serves as a model for the spirit, style, and rhythmic pattern to be emulated.
|
| 113 |
"""
|
| 114 |
|
| 115 |
+
|
| 116 |
+
if model_name in ["gpt-4-0125-preview", "gpt-4", "gpt-3.5-turbo"]:
|
| 117 |
+
response = OPENAI_CLIENT.chat.completions.create(
|
| 118 |
+
model=model_name,
|
| 119 |
+
messages=[
|
| 120 |
+
{"role": "system", "content": system_prompt},
|
| 121 |
+
{"role": "user", "content": prompt}
|
| 122 |
+
],
|
| 123 |
+
max_tokens=1000
|
| 124 |
+
)
|
| 125 |
+
generated_text = response.choices[0].message.content.strip()
|
| 126 |
+
elif model_name == "gemini-pro":
|
| 127 |
+
model_response = GEMINI_MODEL.generate_content(
|
| 128 |
+
f"{system_prompt}, {prompt}"
|
| 129 |
+
)
|
| 130 |
+
generated_text = model_response.candidates[0].content.parts[0].text
|
| 131 |
+
|
| 132 |
generated_article = extract_article_from_content(generated_text)
|
| 133 |
|
| 134 |
validate_article_result = validate_article(generated_article, lesson_words, base_chars, original_word_count)
|
|
|
|
| 212 |
base_chars_input = gr.Textbox(label="Base Characters")
|
| 213 |
|
| 214 |
with gr.Row():
|
| 215 |
+
model_list = ["gpt-4-0125-preview", "gpt-3.5-turbo", "gpt-4", "gemini-pro"]
|
| 216 |
with gr.Column():
|
|
|
|
| 217 |
model_1 = gr.Dropdown(label="Model 1", choices=model_list, value="gpt-4-0125-preview")
|
| 218 |
generate_button1 = gr.Button("Generate Article - gpt-4-0125-preview")
|
| 219 |
with gr.Column():
|
|
|
|
| 237 |
with gr.Column():
|
| 238 |
# validate_article_result_3 Json format
|
| 239 |
validate_article_result_3 = gr.JSON()
|
| 240 |
+
with gr.Row():
|
| 241 |
+
with gr.Column():
|
| 242 |
+
model_4 = gr.Dropdown(label="Model 4", choices=model_list, value="gemini-pro")
|
| 243 |
+
generate_button4 = gr.Button("Generate Article - gemini-pro")
|
| 244 |
+
with gr.Column():
|
| 245 |
+
output_text4 = gr.Textbox(label="Generated Article - gemini-pro")
|
| 246 |
+
with gr.Column():
|
| 247 |
+
validate_article_result_4 = gr.JSON()
|
| 248 |
|
| 249 |
generate_button1.click(
|
| 250 |
generate_new_article,
|
|
|
|
| 261 |
inputs=[lesson_words_input, original_article_input, original_word_count_input, base_chars_input, model_3],
|
| 262 |
outputs=[output_text3, validate_article_result_3]
|
| 263 |
)
|
| 264 |
+
generate_button4.click(
|
| 265 |
+
generate_new_article,
|
| 266 |
+
inputs=[lesson_words_input, original_article_input, original_word_count_input, base_chars_input, model_4],
|
| 267 |
+
outputs=[output_text4, validate_article_result_4]
|
| 268 |
+
)
|
| 269 |
|
| 270 |
# 為其他模型添加點擊事件
|
| 271 |
lesson_csv_file_input.change(
|
requirements.txt
CHANGED
|
@@ -1,3 +1,9 @@
|
|
| 1 |
gradio
|
| 2 |
pandas
|
| 3 |
openai>=1.0.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
pandas
|
| 3 |
openai>=1.0.0
|
| 4 |
+
|
| 5 |
+
google-cloud
|
| 6 |
+
google-cloud-aiplatform
|
| 7 |
+
google-cloud-bigquery
|
| 8 |
+
google-cloud-bigquery[pandas]
|
| 9 |
+
google-cloud-storage
|