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

Added app.py to hf space

Browse files
Files changed (1) hide show
  1. app.py +67 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import random
4
+ import os
5
+
6
+ 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",
14
+ "masturbate", "orgasm", "gangbang", "rape", "incest", "pedophile", "child abuse",
15
+ "bestiality", "prostitution", "threesome", "anal", "oral", "intercourse",
16
+ "ejaculation", "cumshot", "hardcore", "softcore", "lingerie", "stripper",
17
+ "dildo", "vibrator", "panty", "bra", "nipple", "genitals", "pubic", "ass", "tits",
18
+ "dick", "pussy", "blowjob", "handjob", "deepthroat", "g-string", "bikini",
19
+ "topless", "bottomless", "kinky", "dominant", "submissive", "sadism", "masochism",
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)