PSNbst commited on
Commit
9cf392a
·
verified ·
1 Parent(s): 7e2d2e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -23
app.py CHANGED
@@ -1,7 +1,11 @@
1
  import gradio as gr
2
  import random
3
- import openai
4
  import os
 
 
 
 
 
5
 
6
  # ========== 默认选项和数据 ==========
7
  EXPRESSIONS = ["smiling", "determined", "surprised", "serene"]
@@ -35,31 +39,36 @@ def generate_natural_language_description(tags, api_key=None):
35
  """
36
  使用 OpenAI GPT 生成自然语言描述(适配 GPT-4o)。
37
  """
38
- if api_key:
39
- try:
40
- openai.api_key = api_key.strip()
41
- response = openai.ChatCompletion.create(
42
- model="gpt-4",
43
- messages=[
44
- {"role": "system", "content": (
 
 
 
 
 
 
 
 
 
45
  "You are a helpful assistant that generates creative painting/prompt descriptions. "
46
  "Write at least three sentences in English, separated by periods."
47
- )},
48
- {"role": "user", "content": f"Here are the tags: {tags}\nPlease generate a vivid, imaginative scene description."}
49
- ]
50
- )
51
- return response["choices"][0]["message"]["content"].strip()
52
- except Exception as e:
53
- return f"GPT generation failed. Error: {e}"
54
- else:
55
- # 本地逻辑生成
56
- return (
57
- f"In this scene, {tags.get('character_name', 'the character')} appears in a {tags.get('scene', 'mysterious place')}, "
58
- f"captured from a {tags.get('camera_angle', 'unique angle')} perspective. "
59
- f"The overall style combines {tags.get('artist_prompt', 'an unknown artist')} and {tags.get('style', 'a distinctive aesthetic')}, "
60
- f"bringing a captivating atmosphere to the artwork. They are {tags.get('action', 'doing something')} "
61
- f"with a {tags.get('expression', 'neutral expression')}, holding {tags.get('items', 'something')} among {tags.get('other_details', 'subtle details')}."
62
  )
 
 
 
63
 
64
  def generate_prompt(action_file, style_file, artist_files, character_files, api_key, selected_categories):
65
  """
 
1
  import gradio as gr
2
  import random
 
3
  import os
4
+ from openai import OpenAI
5
+ from dotenv import load_dotenv
6
+
7
+ # 加载环境变量
8
+ load_dotenv()
9
 
10
  # ========== 默认选项和数据 ==========
11
  EXPRESSIONS = ["smiling", "determined", "surprised", "serene"]
 
39
  """
40
  使用 OpenAI GPT 生成自然语言描述(适配 GPT-4o)。
41
  """
42
+ # 如果用户未输入 API Key,则尝试从环境变量获取
43
+ if not api_key:
44
+ api_key = os.getenv("OPENAI_API_KEY")
45
+ if not api_key:
46
+ return "Error: No API Key provided and none found in environment variables."
47
+
48
+ try:
49
+ # 初始化 OpenAI 客户端
50
+ client = OpenAI(api_key=api_key)
51
+
52
+ # 调用 chat.completions.create
53
+ response = client.chat.completions.create(
54
+ messages=[
55
+ {
56
+ "role": "system",
57
+ "content": (
58
  "You are a helpful assistant that generates creative painting/prompt descriptions. "
59
  "Write at least three sentences in English, separated by periods."
60
+ ),
61
+ },
62
+ {
63
+ "role": "user",
64
+ "content": f"Here are the tags: {tags}\nPlease generate a vivid, imaginative scene description.",
65
+ },
66
+ ],
67
+ model="gpt-4o",
 
 
 
 
 
 
 
68
  )
69
+ return response["choices"][0]["message"]["content"].strip()
70
+ except Exception as e:
71
+ return f"GPT generation failed. Error: {e}"
72
 
73
  def generate_prompt(action_file, style_file, artist_files, character_files, api_key, selected_categories):
74
  """