PraneshJs commited on
Commit
9b17b37
·
verified ·
1 Parent(s): 41b451d

added negative prompts

Browse files
Files changed (1) hide show
  1. app.py +29 -24
app.py CHANGED
@@ -7,7 +7,8 @@ def process(prompt):
7
  """
8
  Processes the user's text prompt to either generate an image or display a content warning.
9
  """
10
- # Expanded list of restricted words.
 
11
  restricted_words = [
12
  "sex", "erotic", "nude", "explicit", "porn", "xxx", "18+", "nudity", "sexual",
13
  "lewd", "obscene", "hentai", "bondage", "bdsm", "fetish", "voyeur",
@@ -20,48 +21,52 @@ def process(prompt):
20
  "orgies", "sexually suggestive", "vulgar"
21
  ]
22
 
23
- # Check if the user's prompt contains any of the restricted words (case-insensitive).
24
  if any(word in prompt.lower() for word in restricted_words):
25
  return "This is mature content. It's not permitted to generate images on this topic."
26
 
27
- # If the prompt is safe, proceed with image generation.
 
 
 
 
 
 
28
  filename = str(random.randint(111111111, 999999999)) + ".png"
29
  file_path = os.path.join(os.path.dirname(__file__), filename)
30
-
31
- # Use Pollinations API to get the image.
32
- response = requests.get(
33
- "https://image.pollinations.ai/prompt/" + prompt +
34
- "?model=flux-realism&width=2048&height=2048&nologo=true&seed=" +
35
- str(random.randint(0, 999999999))
 
 
 
 
36
  )
37
 
 
 
38
  if response.status_code == 200:
39
  with open(file_path, "wb") as f:
40
  f.write(response.content)
41
  return file_path
42
  else:
43
- # Return an error message if the API call fails.
44
  return f"Error: Could not retrieve image. Status code: {response.status_code}"
45
 
46
- # Define the Gradio interface.
47
  title = "Pollinations Image Generator"
48
  description = "This app generates images from text using the Pollinations API."
49
- article = "Note: This app is filtered to prevent the generation of mature content."
50
 
51
  iface = gr.Interface(
52
- fn=process,
53
- inputs=gr.Textbox(
54
- lines=2,
55
- placeholder="Enter your prompt here...",
56
- label="Prompt"
57
- ),
58
- outputs=gr.Image(
59
- type="filepath",
60
- label="Generated Image"
61
- ),
62
- title=title,
63
  description=description,
64
  article=article
65
  )
66
 
67
- iface.launch(server_name="0.0.0.0", server_port=7860, pwa=True)
 
7
  """
8
  Processes the user's text prompt to either generate an image or display a content warning.
9
  """
10
+
11
+ # Expanded list of restricted words (mature/unsafe content).
12
  restricted_words = [
13
  "sex", "erotic", "nude", "explicit", "porn", "xxx", "18+", "nudity", "sexual",
14
  "lewd", "obscene", "hentai", "bondage", "bdsm", "fetish", "voyeur",
 
21
  "orgies", "sexually suggestive", "vulgar"
22
  ]
23
 
24
+ # Check restricted words
25
  if any(word in prompt.lower() for word in restricted_words):
26
  return "This is mature content. It's not permitted to generate images on this topic."
27
 
28
+ # Add negative prompts to improve quality
29
+ negative_prompt = (
30
+ "blurry, low quality, distorted, watermark, text, cropped, deformed, bad anatomy, "
31
+ "extra limbs, missing limbs, duplicate, pixelated, ugly, disfigured, grainy, noisy"
32
+ )
33
+
34
+ # Generate filename
35
  filename = str(random.randint(111111111, 999999999)) + ".png"
36
  file_path = os.path.join(os.path.dirname(__file__), filename)
37
+
38
+ # Build API request
39
+ api_url = (
40
+ "https://image.pollinations.ai/prompt/"
41
+ + prompt
42
+ + "?model=flux-realism"
43
+ + "&width=2048&height=2048"
44
+ + "&nologo=true"
45
+ + "&seed=" + str(random.randint(0, 999999999))
46
+ + "&negative_prompt=" + negative_prompt.replace(" ", "%20")
47
  )
48
 
49
+ response = requests.get(api_url)
50
+
51
  if response.status_code == 200:
52
  with open(file_path, "wb") as f:
53
  f.write(response.content)
54
  return file_path
55
  else:
 
56
  return f"Error: Could not retrieve image. Status code: {response.status_code}"
57
 
58
+ # Define the Gradio interface
59
  title = "Pollinations Image Generator"
60
  description = "This app generates images from text using the Pollinations API."
61
+ article = "Note: Mature content is blocked. Negative prompts are applied to avoid blurry or low-quality results."
62
 
63
  iface = gr.Interface(
64
+ fn=process,
65
+ inputs=gr.Textbox(lines=2, placeholder="Enter your prompt here...", label="Prompt"),
66
+ outputs=gr.Image(type="filepath", label="Generated Image"),
67
+ title=title,
 
 
 
 
 
 
 
68
  description=description,
69
  article=article
70
  )
71
 
72
+ iface.launch(server_name="0.0.0.0", server_port=7860, pwa=True)