Mahdimtd commited on
Commit
cb2d9e2
·
verified ·
1 Parent(s): 0be097c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -36
app.py CHANGED
@@ -5,50 +5,49 @@ import pytz
5
  import yaml
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
 
5
  import yaml
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
+ from tavily import TavilyClient
 
 
 
 
 
 
 
13
 
14
  @tool
15
+ def search_internet(query: str) -> str:
16
+ """Search the internet using Tavily API to retrieve relevant results for a given query.
17
 
18
  Args:
19
+ query: The search query string to look up information about.
 
 
 
 
 
 
20
  """
21
+ print(f"🔍 Searching with Tavily: {query}")
22
+
23
+ # Get API key from tavily.com (1000 free searches/month)
24
+ api_key = "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"
25
+
26
+ try:
27
+ client = TavilyClient(api_key=api_key)
28
+
29
+ response = client.search(
30
+ query=query,
31
+ max_results=3
32
+ )
33
+
34
+ results = response.get('results', [])
35
+
36
+ if not results:
37
+ return "No results found."
38
+
39
+ output = []
40
+ for i, result in enumerate(results, 1):
41
+ title = result.get('title', 'No title')
42
+ content = result.get('content', 'No description')
43
+ url = result.get('url', '')
44
+ output.append(f"{i}. {title}\n{content[:200]}...\nSource: {url}\n")
45
+
46
+ return "\n".join(output)
47
+
48
+ except Exception as e:
49
+ return f"Search error: {str(e)}"
50
+
51
 
52
 
53
  @tool