arif670 commited on
Commit
189d366
·
verified ·
1 Parent(s): 288ba66

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -5
app.py CHANGED
@@ -3,6 +3,7 @@ import requests
3
  from PIL import Image
4
  from io import BytesIO
5
  import os
 
6
 
7
  # Hugging Face API endpoint and model (Stable Diffusion in this case)
8
  HF_MODEL_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2"
@@ -10,7 +11,25 @@ HF_MODEL_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-d
10
  # Get API key securely from environment variable
11
  HF_API_KEY = os.getenv("HF_API_KEY")
12
 
13
- # Function to generate image based on topic
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def generate_image(topic):
15
  # Prepare the payload for the Hugging Face API
16
  payload = {
@@ -31,16 +50,35 @@ def generate_image(topic):
31
  else:
32
  return "Error generating image. Please try again."
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  # Build the Gradio interface
35
  interface = gr.Interface(
36
- fn=generate_image,
37
  inputs=gr.Textbox(label="Enter Topic (e.g., Dog, Space, Nature)"),
38
- outputs=gr.Image(),
39
  live=True,
40
- title="Children's School Project Image Generator",
41
- description="Enter a topic and generate an image related to it for school projects. Example topics: Dog, Space, Nature."
42
  )
43
 
44
  # Launch the app
45
  interface.launch(share=True)
46
 
 
 
3
  from PIL import Image
4
  from io import BytesIO
5
  import os
6
+ from bs4 import BeautifulSoup
7
 
8
  # Hugging Face API endpoint and model (Stable Diffusion in this case)
9
  HF_MODEL_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2"
 
11
  # Get API key securely from environment variable
12
  HF_API_KEY = os.getenv("HF_API_KEY")
13
 
14
+ # Function to perform image search using DuckDuckGo and return image URL
15
+ def search_images(topic):
16
+ search_url = f"https://duckduckgo.com/html/?q={topic}+images"
17
+
18
+ # Send the request to DuckDuckGo and retrieve search results
19
+ response = requests.get(search_url)
20
+ soup = BeautifulSoup(response.text, "html.parser")
21
+
22
+ # Find all image links in the search results
23
+ image_urls = []
24
+ for img in soup.find_all("img", {"class": "tile--img__img"}):
25
+ img_url = img.get("src")
26
+ if img_url and img_url.startswith("http"):
27
+ image_urls.append(img_url)
28
+
29
+ # Return the first 3 image URLs as potential suggestions
30
+ return image_urls[:3]
31
+
32
+ # Function to generate image based on topic using Hugging Face API (Stable Diffusion)
33
  def generate_image(topic):
34
  # Prepare the payload for the Hugging Face API
35
  payload = {
 
50
  else:
51
  return "Error generating image. Please try again."
52
 
53
+ # Function to combine image search and generation
54
+ def generate_and_search_images(topic):
55
+ # Generate an image using Hugging Face (Stable Diffusion)
56
+ generated_image = generate_image(topic)
57
+
58
+ # Perform image search using DuckDuckGo
59
+ search_results = search_images(topic)
60
+
61
+ # Display the results
62
+ images = []
63
+ for url in search_results:
64
+ # Download the image from the URL
65
+ img_response = requests.get(url)
66
+ img = Image.open(BytesIO(img_response.content))
67
+ images.append(img)
68
+
69
+ return generated_image, images
70
+
71
  # Build the Gradio interface
72
  interface = gr.Interface(
73
+ fn=generate_and_search_images,
74
  inputs=gr.Textbox(label="Enter Topic (e.g., Dog, Space, Nature)"),
75
+ outputs=[gr.Image(label="Generated Image"), gr.Gallery(label="Suggested Images")],
76
  live=True,
77
+ title="Children's School Project Image Generator and Image Search",
78
+ description="Enter a topic and generate an image related to it for school projects. Additionally, get suggested images based on the topic."
79
  )
80
 
81
  # Launch the app
82
  interface.launch(share=True)
83
 
84
+