mingjunji commited on
Commit
a70eb27
·
verified ·
1 Parent(s): 2caa83b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -16
app.py CHANGED
@@ -3,6 +3,16 @@ import google.generativeai as genai
3
  import os
4
  from PIL import Image
5
 
 
 
 
 
 
 
 
 
 
 
6
  # 配置 Gemini API
7
  # 自动从您设置的环境变量中读取 Key
8
  api_key = os.getenv("MY_API_KEY")
@@ -21,14 +31,13 @@ def analyze_lighting(image):
21
 
22
  # 编写针对灯光艺术师的专业 Prompt
23
  prompt = """
24
- 你是一位精通 Unreal Engine 5 的资深灯光艺术家
25
- 请对这张场景截图进行专业灯光分析,并给出具体优化建议:
26
- 1. 氛围分析:色温对比、全局光照(GI)填充情况。
27
- 2. 技术评估:曝光是否过爆、部细节是否丢失、阴影质量。
28
- 3. 材质反馈:光线在不同材质如大理石金属上的表现。
29
- 4. 改进建议:是否需要增加积雾(Volumetric Fog)、虚幻引擎中补光(Fill Light)或轮廓光。
30
- 请用专业、简洁的中文回答。
31
- """
32
 
33
  # 发送图片和文字
34
  response = model.generate_content([prompt, image])
@@ -37,17 +46,48 @@ def analyze_lighting(image):
37
  except Exception as e:
38
  return f"Gemini 分析出错: {str(e)}"
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  # 构建 Gradio 界面
41
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
42
- gr.Markdown("# 💡 UE5 Lighting Artist AI Assistant (Gemini 版)")
 
 
43
 
44
  with gr.Row():
45
- with gr.Column(scale=1):
46
- input_img = gr.Image(label="上传截图", type="pil")
47
- btn = gr.Button("生成灯光分析报告", variant="primary")
48
- with gr.Column(scale=1):
49
- output_text = gr.Markdown("分析结果将在此显示...")
 
 
50
 
51
- btn.click(analyze_lighting, inputs=[input_img], outputs=[output_text])
 
 
 
 
 
 
52
 
53
  demo.launch()
 
3
  import os
4
  from PIL import Image
5
 
6
+
7
+ # 这里的 CSS 定义了卡片的颜色、边框和图标风格
8
+ custom_css = """
9
+ .report-card { background: #1a1c1e; border-left: 5px solid #3b82f6; padding: 15px; border-radius: 8px; margin-bottom: 15px; color: white; }
10
+ .score-box { font-size: 24px; font-weight: bold; color: #10b981; margin-bottom: 10px; }
11
+ .tag { display: inline-block; background: #334155; padding: 2px 8px; border-radius: 4px; font-size: 12px; margin-right: 5px; }
12
+ .suggestion-item { margin-bottom: 10px; padding: 10px; background: #2d3748; border-radius: 5px; }
13
+ """
14
+
15
+
16
  # 配置 Gemini API
17
  # 自动从您设置的环境变量中读取 Key
18
  api_key = os.getenv("MY_API_KEY")
 
31
 
32
  # 编写针对灯光艺术师的专业 Prompt
33
  prompt = """
34
+ 作为 UE5 灯光总监,分析截图请严格按照以下格式输出分析结果:
35
+ [SCORE] 0-100数字
36
+ [COLORS] 识别出主色调(用逗号隔开)
37
+ [ANALYSIS] 氛围与明的深度分析
38
+ [TECH] 技术建议体积雾GI等
39
+ [IMPROVE] 体的 3 条优化建议(每条建议用“|”分隔)
40
+ """
 
41
 
42
  # 发送图片和文字
43
  response = model.generate_content([prompt, image])
 
46
  except Exception as e:
47
  return f"Gemini 分析出错: {str(e)}"
48
 
49
+
50
+ def format_output(ai_text):
51
+ # 简单的文本切割逻辑(可以根据实际 Prompt 调整)
52
+ try:
53
+ # 这里仅为逻辑演示,实际可以使用正则表达式提取
54
+ score = "85" # 假设提取出的分数
55
+ html = f"""
56
+ <div class="report-card">
57
+ <div class="score-box">💡 场景灯光评分:{score}</div>
58
+ <div class="tag">UE5.4</div><div class="tag">Lumen</div><div class="tag">Realistic</div>
59
+ <hr style="border: 0.5px solid #444; margin: 15px 0;">
60
+ <div style="color: #60a5fa; font-weight: bold;">📊 深度分析:</div>
61
+ <p>{ai_text[:200]}...</p>
62
+ </div>
63
+ """
64
+ return html
65
+ except:
66
+ return ai_text
67
+
68
+
69
+
70
  # 构建 Gradio 界面
71
+
72
+
73
+ with gr.Blocks(css=custom_css, theme=gr.themes.Default(primary_hue="blue", neutral_hue="slate")) as demo:
74
+ gr.HTML("<h1 style='text-align: center; color: white;'>GameLight Analyzer Pro</h1>")
75
 
76
  with gr.Row():
77
+ with gr.Column(scale=4):
78
+ input_img = gr.Image(label="UE5 Scene Capture", type="pil", height=500)
79
+ btn = gr.Button("🔍 开始深度分析", variant="primary")
80
+
81
+ with gr.Column(scale=5):
82
+ # 使用 gr.HTML 而不是 gr.Markdown
83
+ output_panel = gr.HTML("<div style='color: #888;'>等待分析数据...</div>")
84
 
85
+ # 点击后的逻辑:调用 Gemini -> 格式化为 HTML -> 显示
86
+ def run_analysis(img):
87
+ raw_res = analyze_lighting(img) # 调用你之前的 Gemini 函数
88
+ # 这里的 format_to_pro_ui 是一个将纯文本转为漂亮 HTML 的函数
89
+ return format_to_pro_ui(raw_res)
90
+
91
+ btn.click(run_analysis, inputs=[input_img], outputs=[output_panel])
92
 
93
  demo.launch()