hadadrjt commited on
Commit
0e93d21
·
1 Parent(s): 3949dad

ai: Enable authentication for Pollinations AI.

Browse files

Required for audio and image generation.

* Goodbye anonymous.

Files changed (3) hide show
  1. src/tools/audio.py +4 -2
  2. src/tools/image.py +2 -1
  3. src/utils/tools.py +4 -3
src/tools/audio.py CHANGED
@@ -40,7 +40,7 @@ class AudioGeneration:
40
  generate_audio_instruct = quote(generate_audio_instruction)
41
 
42
  # Initialize tools and extract the audio generation service endpoint (third element in the returned tuple)
43
- _, _, audio_tool = initialize_tools()
44
 
45
  # Construct the full URL by appending the encoded instruction to the base audio tool URL
46
  url = f"{audio_tool}/{generate_audio_instruct}"
@@ -57,6 +57,7 @@ class AudioGeneration:
57
  while True:
58
  # Generate a random IP address to spoof the client's origin in the request headers
59
  headers = {
 
60
  "X-Forwarded-For": generate_ip() # Set the X-Forwarded-For header to a random IP address
61
  }
62
 
@@ -92,7 +93,7 @@ class AudioGeneration:
92
  generate_audio_instruct = quote(generate_audio_instruction)
93
 
94
  # Initialize tools and get audio generation endpoint
95
- _, _, audio_tool = initialize_tools()
96
 
97
  # Construct request URL
98
  url = f"{audio_tool}/{generate_audio_instruct}"
@@ -109,6 +110,7 @@ class AudioGeneration:
109
  while True:
110
  # Define HTTP headers for the request, including random IP address to simulate different client origins
111
  headers = {
 
112
  "X-Forwarded-For": generate_ip() # Generate and set a random IP address for the request header
113
  }
114
 
 
40
  generate_audio_instruct = quote(generate_audio_instruction)
41
 
42
  # Initialize tools and extract the audio generation service endpoint (third element in the returned tuple)
43
+ _, _, audio_tool, poll_token = initialize_tools()
44
 
45
  # Construct the full URL by appending the encoded instruction to the base audio tool URL
46
  url = f"{audio_tool}/{generate_audio_instruct}"
 
57
  while True:
58
  # Generate a random IP address to spoof the client's origin in the request headers
59
  headers = {
60
+ "Authorization": f"Bearer {poll_token}", # Pollinations AI API Token
61
  "X-Forwarded-For": generate_ip() # Set the X-Forwarded-For header to a random IP address
62
  }
63
 
 
93
  generate_audio_instruct = quote(generate_audio_instruction)
94
 
95
  # Initialize tools and get audio generation endpoint
96
+ _, _, audio_tool, poll_token = initialize_tools()
97
 
98
  # Construct request URL
99
  url = f"{audio_tool}/{generate_audio_instruct}"
 
110
  while True:
111
  # Define HTTP headers for the request, including random IP address to simulate different client origins
112
  headers = {
113
+ "Authorization": f"Bearer {poll_token}", # Pollinations AI API Token
114
  "X-Forwarded-For": generate_ip() # Generate and set a random IP address for the request header
115
  }
116
 
src/tools/image.py CHANGED
@@ -73,7 +73,7 @@ class ImageGeneration:
73
  width, height = ImageGeneration.FORMATS[image_format]
74
 
75
  # Initialize tools and get image generation service endpoint URL
76
- _, image_tool, _ = initialize_tools()
77
 
78
  # Encode instruction safely for URL path usage
79
  generate_image_instruct = quote(generate_image_instruction)
@@ -97,6 +97,7 @@ class ImageGeneration:
97
 
98
  # Prepare headers
99
  headers = {
 
100
  "X-Forwarded-For": generate_ip() # Random IP address for request header to simulate client origin
101
  }
102
 
 
73
  width, height = ImageGeneration.FORMATS[image_format]
74
 
75
  # Initialize tools and get image generation service endpoint URL
76
+ _, image_tool, poll_token, _ = initialize_tools()
77
 
78
  # Encode instruction safely for URL path usage
79
  generate_image_instruct = quote(generate_image_instruction)
 
97
 
98
  # Prepare headers
99
  headers = {
100
+ "Authorization": f"Bearer {poll_token}", # Pollinations AI API Token
101
  "X-Forwarded-For": generate_ip() # Random IP address for request header to simulate client origin
102
  }
103
 
src/utils/tools.py CHANGED
@@ -51,13 +51,14 @@ def initialize_tools():
51
  tool_setup = selected.get("done") # Main tool setup endpoint or configuration
52
  image_tool = selected.get("image") # Image generation service endpoint
53
  audio_tool = selected.get("audio") # Audio generation service endpoint
 
54
 
55
  # Verify that all required tool endpoints are present, raise exception if any is missing
56
- if not tool_setup or not image_tool or not audio_tool:
57
  raise Exception("Selected host is missing required tool endpoints.")
58
 
59
  # Return the three tool endpoints as a tuple
60
- return tool_setup, image_tool, audio_tool
61
 
62
  # Initialize the tools by selecting an available host and retrieving its endpoints
63
- tool_setup, image_tool, audio_tool = initialize_tools()
 
51
  tool_setup = selected.get("done") # Main tool setup endpoint or configuration
52
  image_tool = selected.get("image") # Image generation service endpoint
53
  audio_tool = selected.get("audio") # Audio generation service endpoint
54
+ poll_token = selected.get("pol") # Pollinations AI API Token
55
 
56
  # Verify that all required tool endpoints are present, raise exception if any is missing
57
+ if not tool_setup or not image_tool or not audio_tool or not poll_token:
58
  raise Exception("Selected host is missing required tool endpoints.")
59
 
60
  # Return the three tool endpoints as a tuple
61
+ return tool_setup, image_tool, audio_tool, poll_token
62
 
63
  # Initialize the tools by selecting an available host and retrieving its endpoints
64
+ tool_setup, image_tool, audio_tool, poll_token = initialize_tools()