Mr-Vicky-01 commited on
Commit
1e8f60b
1 Parent(s): 5acfed5

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +34 -0
  2. image_engine.py +35 -0
  3. llm.py +26 -0
  4. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from llm import Model
3
+ from image_engine import ImageGenerator
4
+ import io
5
+
6
+ # Instantiate the models
7
+ gemini_model = Model()
8
+ image_generator = ImageGenerator()
9
+
10
+ # Streamlit interface
11
+ st.title("AI Arist🎨")
12
+ art_idea = st.text_input("Enter your drawing idea:")
13
+
14
+ if st.button("Generate Art"):
15
+ if art_idea:
16
+ with st.spinner("Enhancing your idea..."):
17
+ enhanced_idea = gemini_model.enhance_idea(art_idea)
18
+ st.write("Enhanced Idea:", enhanced_idea)
19
+ with st.spinner("Geneating an image..."):
20
+ image = image_generator.generate_image(enhanced_idea)
21
+ st.image(image, caption="Generated Art")
22
+
23
+ # Add a download button for the generated image
24
+ img_byte_arr = io.BytesIO()
25
+ image.save(img_byte_arr, format='PNG')
26
+ img_byte_arr = img_byte_arr.getvalue()
27
+ st.download_button(
28
+ label="Download",
29
+ data=img_byte_arr,
30
+ file_name="generated_art.png",
31
+ mime="image/png"
32
+ )
33
+ else:
34
+ st.error("Please enter an art idea!")
image_engine.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ import requests
3
+ import io
4
+ from dotenv import load_dotenv
5
+ import os
6
+
7
+ load_dotenv()
8
+
9
+ # Define the ImageGenerator class for Hugging Face API
10
+ class ImageGenerator:
11
+ def __init__(self) -> None:
12
+ self.api_url = "https://api-inference.huggingface.co/models/sd-community/sdxl-flash"
13
+ self.headers = {"Authorization": f"Bearer {os.getenv('HUGGING_FACE')}"}
14
+
15
+ def generate_image(self, prompt):
16
+ payload = {
17
+ "inputs": prompt,
18
+ # "negative_prompt": "ugly, distorted, low quality"
19
+ }
20
+ response = requests.post(self.api_url, headers=self.headers, json=payload)
21
+
22
+ # Debugging: Print response status and content type
23
+ print(f"Response Status Code: {response.status_code}")
24
+ print(f"Response Content Type: {response.headers.get('Content-Type')}")
25
+
26
+ # Check for valid response
27
+ if response.status_code == 200 and response.headers.get('Content-Type').startswith('image'):
28
+ image_bytes = response.content
29
+ image = Image.open(io.BytesIO(image_bytes))
30
+ return image
31
+ else:
32
+ # Print the error message if not a valid image
33
+ print("Error: Invalid response received from API")
34
+ print(response.text) # Print the text content of the response for debugging
35
+ raise Exception("Failed to generate image from API")
llm.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import google.generativeai as genai
2
+ from dotenv import load_dotenv
3
+ import os
4
+
5
+
6
+ # Load environment variables
7
+ load_dotenv()
8
+ os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")
9
+ genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
10
+
11
+ # Define the GeminiModel class for Gemini LLM
12
+ class Model:
13
+ def __init__(self) -> None:
14
+ self.model = genai.GenerativeModel('gemini-1.5-flash-latest')
15
+
16
+ def enhance_idea(self, idea):
17
+ prompt = """Give me the Pencil art prompt with this idea using this key words [Contrast: Utilize varying shades of gray to create depth and dimension.
18
+ Texture: Capture the texture of different surfaces like wood, fabric, or skin.
19
+ Detail: Focus on intricate details to enhance realism.
20
+ Shading: Use different pencil pressures to create smooth gradients and sharp contrasts.
21
+ Light and Shadow: Create the illusion of light sources and how they interact with objects.
22
+ Composition: Arrange elements within the drawing to create a visually appealing layout.
23
+ Strokes: Experiment with different pencil strokes (e.g., hatching, cross-hatching, stippling) for texture and shading.
24
+ Negative Space: Use empty areas to define shapes and create balance.], \n Idea: """ + idea + "\n important note: your response must only contain the idea of classic B&W pencil art, without unnecessary elements like ('Enhanced Art Idea:' and markdown format like '##', '*')"
25
+ response = self.model.generate_content([prompt])
26
+ return response.text
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ streamlit
2
+ google-generativeai
3
+ python-dotenv
4
+ requests
5
+ pillow