Mahdimtd commited on
Commit
b50f4cc
·
verified ·
1 Parent(s): 4fa4ddd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -35
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 search_internet(query: str) -> str:
13
- """Search the internet using Tavily API to retrieve relevant results for a given query.
14
 
15
  Args:
16
- query: The search query string to look up information about.
 
 
 
 
 
 
17
  """
18
- print(f"🔍 Searching with Tavily: {query}")
19
-
20
- # Get API key from tavily.com (1000 free searches/month)
21
- api_key = "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"
22
-
23
- try:
24
- client = TavilyClient(api_key=api_key)
25
-
26
- response = client.search(
27
- query=query,
28
- max_results=3
29
- )
30
-
31
- results = response.get('results', [])
32
-
33
- if not results:
34
- return "No results found."
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