Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 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 |
-
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
|
| 44 |
with gr.Row():
|
| 45 |
-
with gr.Column(scale=
|
| 46 |
-
input_img = gr.Image(label="
|
| 47 |
-
btn = gr.Button("
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|