Zenithwang commited on
Commit
79cceb8
·
verified ·
1 Parent(s): 5d4f64c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -18
app.py CHANGED
@@ -1,21 +1,41 @@
1
- import os
2
  import io
 
3
  import base64
4
  import gradio as gr
5
  from PIL import Image
6
  from openai import OpenAI
7
 
8
- def pil_to_data_url(img: Image.Image, fmt="PNG"):
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  buf = io.BytesIO()
10
  img.save(buf, format=fmt)
11
  b64 = base64.b64encode(buf.getvalue()).decode("utf-8")
12
  mime = "image/png" if fmt.upper() == "PNG" else "image/jpeg"
13
  return f"data:{mime};base64,{b64}"
14
 
15
- client = OpenAI(
16
- api_key=os.getenv("STEPFUN_KEY"), # 在 HF Secrets 里配置
17
- base_url="https://platform.stepfun.com/v1",
18
- )
 
 
 
 
 
 
 
19
 
20
  def chat_with_step3(image: Image.Image, question: str):
21
  if image is None:
@@ -23,7 +43,8 @@ def chat_with_step3(image: Image.Image, question: str):
23
  if not question:
24
  question = "请描述这张图片。"
25
 
26
- data_url = pil_to_data_url(image, fmt="PNG")
 
27
 
28
  messages = [
29
  {
@@ -34,22 +55,30 @@ def chat_with_step3(image: Image.Image, question: str):
34
  ],
35
  }
36
  ]
37
- resp = client.chat.completions.create(
38
- model="step3-fp8", # 更稳妥的模型名
39
- messages=messages,
40
- max_tokens=1024,
41
- )
42
- return resp.choices[0].message.content
43
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  iface = gr.Interface(
45
  fn=chat_with_step3,
46
- inputs=[gr.Image(type="pil", label="Upload image"),
47
- gr.Textbox(label="Question")],
48
- outputs="text",
 
 
49
  title="Step3 FP8 (API) Demo",
50
- description="使用 StepFun 的 OpenAI 兼容 API 调用 step3-fp8 模型。"
51
  )
52
 
53
  if __name__ == "__main__":
54
- # HF Spaces 内不必 share=True;保持默认即可
55
  iface.launch()
 
 
1
  import io
2
+ import os
3
  import base64
4
  import gradio as gr
5
  from PIL import Image
6
  from openai import OpenAI
7
 
8
+ # ------- 配置区 -------
9
+ # 推荐在 HF Space 的 Settings → Variables and secrets 里设置:
10
+ # Name: OPENAI_API_KEY Value: 你的 StepFun API Key
11
+ # 如需自定义变量名(比如 STEPFUN_KEY),下面会做兼容处理。
12
+ STEPFUN_ENDPOINT = "https://platform.stepfun.com/v1"
13
+ MODEL_NAME = "step3-fp8"
14
+ MAX_TOKENS = 1024
15
+ # ---------------------
16
+
17
+ def _get_api_key():
18
+ # 优先用 OPENAI_API_KEY(OpenAI SDK 的默认约定),否则回退 STEPFUN_KEY
19
+ return os.getenv("OPENAI_API_KEY") or os.getenv("STEPFUN_KEY")
20
+
21
+ def _pil_to_data_url(img: Image.Image, fmt: str = "PNG") -> str:
22
  buf = io.BytesIO()
23
  img.save(buf, format=fmt)
24
  b64 = base64.b64encode(buf.getvalue()).decode("utf-8")
25
  mime = "image/png" if fmt.upper() == "PNG" else "image/jpeg"
26
  return f"data:{mime};base64,{b64}"
27
 
28
+ def _make_client() -> OpenAI:
29
+ key = _get_api_key()
30
+ if not key:
31
+ # 给出更友好的报错,避免默认的 OpenAIError 让人摸不着头脑
32
+ raise RuntimeError(
33
+ "API Key 未设置。请到 Space 的 Settings → Variables and secrets 添加:\n"
34
+ "Name=OPENAI_API_KEY,Value=你的 StepFun API Key(或用 STEPFUN_KEY 也可)。"
35
+ )
36
+ return OpenAI(api_key=key, base_url=STEPFUN_ENDPOINT)
37
+
38
+ client = _make_client()
39
 
40
  def chat_with_step3(image: Image.Image, question: str):
41
  if image is None:
 
43
  if not question:
44
  question = "请描述这张图片。"
45
 
46
+ # PIL 图转成 data URL,传到 OpenAI 兼容接口
47
+ data_url = _pil_to_data_url(image, fmt="PNG")
48
 
49
  messages = [
50
  {
 
55
  ],
56
  }
57
  ]
 
 
 
 
 
 
58
 
59
+ try:
60
+ resp = client.chat.completions.create(
61
+ model=MODEL_NAME,
62
+ messages=messages,
63
+ max_tokens=MAX_TOKENS,
64
+ )
65
+ return resp.choices[0].message.content
66
+ except Exception as e:
67
+ # 返回更清晰的错误信息到前端,方便你在 Space 控制台外定位问题
68
+ return f"调用失败:{e}"
69
+
70
+ # 用 Interface 足够简单,也可改 Blocks
71
  iface = gr.Interface(
72
  fn=chat_with_step3,
73
+ inputs=[
74
+ gr.Image(type="pil", label="Upload image"),
75
+ gr.Textbox(label="Question", placeholder="问点什么…"),
76
+ ],
77
+ outputs=gr.Textbox(label="Answer"),
78
  title="Step3 FP8 (API) Demo",
79
+ description="使用 StepFun 的 OpenAI 兼容 API(/v1)来调用 step3-fp8 模型。",
80
  )
81
 
82
  if __name__ == "__main__":
83
+ # HF Spaces 里不需要 share=True
84
  iface.launch()