eeenn commited on
Commit
eab2a01
·
verified ·
1 Parent(s): 7835f0a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -1
app.py CHANGED
@@ -33,6 +33,29 @@ def get_current_time_in_timezone(timezone: str) -> str:
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  final_answer = FinalAnswerTool()
38
 
@@ -55,7 +78,7 @@ with open("prompts.yaml", 'r') as stream:
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer, DuckDuckGoSearchTool(), image_generation_tool, get_current_time_in_timezone], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
 
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
36
+ @tool
37
+ def text_to_image(prompt: str) -> AgentImage:
38
+ """Generate an image from text via Hugging Face Inference API (sdxl-turbo).
39
+ Args:
40
+ prompt: The image description in English.
41
+ """
42
+ hf_token = os.getenv("HF_TOKEN")
43
+ if not hf_token:
44
+ raise RuntimeError("HF_TOKEN is missing. Add it in Settings → Variables and Secrets.")
45
+
46
+ headers = {
47
+ "Authorization": f"Bearer {hf_token}",
48
+ "Accept": "image/png"
49
+ }
50
+ # 你也可以换成其它支持推理的模型,比如:black-forest-labs/FLUX.1-dev
51
+ url = "https://api-inference.huggingface.co/models/stabilityai/sdxl-turbo"
52
+
53
+ resp = requests.post(url, headers=headers, json={"inputs": prompt}, timeout=120)
54
+ if resp.status_code != 200:
55
+ raise RuntimeError(f"Inference API error {resp.status_code}: {resp.text[:200]}")
56
+
57
+ img = Image.open(io.BytesIO(resp.content)).convert("RGB")
58
+ return AgentImage(img)
59
 
60
  final_answer = FinalAnswerTool()
61
 
 
78
 
79
  agent = CodeAgent(
80
  model=model,
81
+ tools=[final_answer, DuckDuckGoSearchTool(), image_generation_tool, get_current_time_in_timezone, text_to_image ], ## add your tools here (don't remove final answer)
82
  max_steps=6,
83
  verbosity_level=1,
84
  grammar=None,