Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,46 +6,49 @@ import yaml
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
from tavily import TavilyClient
|
| 9 |
-
|
|
|
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
@tool
|
| 12 |
-
def
|
| 13 |
-
"""
|
| 14 |
|
| 15 |
Args:
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
"""
|
| 18 |
-
print(f"
|
| 19 |
-
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
output = []
|
| 37 |
-
for i, result in enumerate(results, 1):
|
| 38 |
-
title = result.get('title', 'No title')
|
| 39 |
-
content = result.get('content', 'No description')
|
| 40 |
-
url = result.get('url', '')
|
| 41 |
-
output.append(f"{i}. {title}\n{content[:200]}...\nSource: {url}\n")
|
| 42 |
-
|
| 43 |
-
return "\n".join(output)
|
| 44 |
-
|
| 45 |
-
except Exception as e:
|
| 46 |
-
return f"Search error: {str(e)}"
|
| 47 |
-
|
| 48 |
-
|
| 49 |
|
| 50 |
|
| 51 |
@tool
|
|
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
from tavily import TavilyClient
|
| 9 |
+
import torch
|
| 10 |
+
from diffusers import FluxPipeline
|
| 11 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# فقط یک بار لود میشه (برای سرعت)
|
| 15 |
+
pipe = FluxPipeline.from_pretrained(
|
| 16 |
+
"black-forest-labs/FLUX.1-schnell",
|
| 17 |
+
torch_dtype=torch.bfloat16
|
| 18 |
+
)
|
| 19 |
+
pipe.enable_model_cpu_offload() # خیلی مهم! باعث میشه روی CPU هم کار کنه
|
| 20 |
+
|
| 21 |
@tool
|
| 22 |
+
def generate_image(prompt: str, width: int = 1024, height: int = 1024, steps: int = 4) -> str:
|
| 23 |
+
"""Generates an image using FLUX.1-schnell based on the given prompt.
|
| 24 |
|
| 25 |
Args:
|
| 26 |
+
prompt: The text description of the image you want to generate.
|
| 27 |
+
width: Image width in pixels (default: 1024)
|
| 28 |
+
height: Image height in pixels (default: 1024)
|
| 29 |
+
steps: Number of inference steps (4 is enough for schnell, more = better quality but slower)
|
| 30 |
+
|
| 31 |
+
Returns:
|
| 32 |
+
URL of the generated image hosted on the Space
|
| 33 |
"""
|
| 34 |
+
print(f"Generating image with prompt: {prompt}")
|
| 35 |
+
|
| 36 |
+
# تولید عکس
|
| 37 |
+
image = pipe(
|
| 38 |
+
prompt=prompt,
|
| 39 |
+
height=height,
|
| 40 |
+
width=width,
|
| 41 |
+
num_inference_steps=steps,
|
| 42 |
+
guidance_scale=0.0, # برای schnell باید 0 باشه
|
| 43 |
+
max_sequence_length=256
|
| 44 |
+
).images[0]
|
| 45 |
+
|
| 46 |
+
# ذخیره موقت و برگرداندن URL
|
| 47 |
+
image_path = "/tmp/generated_image.png"
|
| 48 |
+
image.save(image_path)
|
| 49 |
+
|
| 50 |
+
# این ترفند باعث میشه عکس در گرادیو نمایش داده بشه
|
| 51 |
+
return image_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
|
| 54 |
@tool
|