Spaces:
Running
Running
usmanyousaf
commited on
Commit
β’
1ad0307
1
Parent(s):
e1b32d6
Update app.py
Browse files
app.py
CHANGED
@@ -2,13 +2,19 @@ import streamlit as st
|
|
2 |
import requests
|
3 |
from PIL import Image
|
4 |
from io import BytesIO
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
# Set Streamlit page configuration
|
7 |
st.set_page_config(
|
8 |
page_title="Sketch to Image using GAN",
|
9 |
layout="centered",
|
10 |
page_icon="ποΈ",
|
11 |
-
initial_sidebar_state="expanded",
|
12 |
)
|
13 |
|
14 |
# Custom CSS for styling
|
@@ -36,13 +42,12 @@ st.markdown(
|
|
36 |
unsafe_allow_html=True,
|
37 |
)
|
38 |
|
39 |
-
|
40 |
-
|
41 |
# Title with colors and emojis
|
42 |
st.markdown("<h1 style='text-align: center; color: #ff6347;'>Sketch to Image using GAN ποΈ</h1>", unsafe_allow_html=True)
|
43 |
|
44 |
# Description Section
|
45 |
st.markdown("<h2 style='text-align: center; color: #ff6347;'>Empowering Multiple Fields with GANs π</h2>", unsafe_allow_html=True)
|
|
|
46 |
# Logo Image
|
47 |
logo_image = Image.open("home1.jpeg")
|
48 |
st.image(logo_image, width=300)
|
@@ -63,19 +68,54 @@ if uploaded_file is not None:
|
|
63 |
if st.button('Generate π'):
|
64 |
# Display a message while generating the image
|
65 |
with st.spinner('Wait for it... Generating your image π¨'):
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import requests
|
3 |
from PIL import Image
|
4 |
from io import BytesIO
|
5 |
+
from fastapi import FastAPI, File, UploadFile
|
6 |
+
from fastapi.responses import StreamingResponse
|
7 |
+
from keras.models import load_model
|
8 |
+
import numpy as np
|
9 |
+
import io
|
10 |
+
import warnings
|
11 |
|
12 |
# Set Streamlit page configuration
|
13 |
st.set_page_config(
|
14 |
page_title="Sketch to Image using GAN",
|
15 |
layout="centered",
|
16 |
page_icon="ποΈ",
|
17 |
+
initial_sidebar_state="expanded",
|
18 |
)
|
19 |
|
20 |
# Custom CSS for styling
|
|
|
42 |
unsafe_allow_html=True,
|
43 |
)
|
44 |
|
|
|
|
|
45 |
# Title with colors and emojis
|
46 |
st.markdown("<h1 style='text-align: center; color: #ff6347;'>Sketch to Image using GAN ποΈ</h1>", unsafe_allow_html=True)
|
47 |
|
48 |
# Description Section
|
49 |
st.markdown("<h2 style='text-align: center; color: #ff6347;'>Empowering Multiple Fields with GANs π</h2>", unsafe_allow_html=True)
|
50 |
+
|
51 |
# Logo Image
|
52 |
logo_image = Image.open("home1.jpeg")
|
53 |
st.image(logo_image, width=300)
|
|
|
68 |
if st.button('Generate π'):
|
69 |
# Display a message while generating the image
|
70 |
with st.spinner('Wait for it... Generating your image π¨'):
|
71 |
+
try:
|
72 |
+
# Prepare the file for sending
|
73 |
+
files = {"file": uploaded_file.getvalue()}
|
74 |
+
|
75 |
+
# Send POST request to FastAPI server
|
76 |
+
response = requests.post("http://127.0.0.1:8000/generate-image/", files=files)
|
77 |
+
|
78 |
+
if response.status_code == 200:
|
79 |
+
# Convert the response content to an image
|
80 |
+
generated_image = Image.open(BytesIO(response.content))
|
81 |
+
|
82 |
+
# Display the generated image in the center
|
83 |
+
col1, col2, col3 = st.columns([1, 2, 1])
|
84 |
+
with col2:
|
85 |
+
st.image(generated_image, caption="Generated Image β¨", width=300)
|
86 |
+
else:
|
87 |
+
st.error("Error in image generation π’")
|
88 |
+
except requests.ConnectionError:
|
89 |
+
st.error("Unable to connect to the FastAPI server. Please make sure it is running.")
|
90 |
+
|
91 |
+
# FastAPI Section
|
92 |
+
warnings.filterwarnings('ignore')
|
93 |
+
generator_model = load_model('last_13k_data_generator_model.h5') # Update this with your generator model's path
|
94 |
+
app = FastAPI()
|
95 |
+
|
96 |
+
@app.post("/generate-image/")
|
97 |
+
async def generate_image(file: UploadFile = File(...)):
|
98 |
+
contents = await file.read()
|
99 |
+
image = Image.open(io.BytesIO(contents)).convert('RGB')
|
100 |
+
image = image.resize((256, 256))
|
101 |
+
|
102 |
+
image_array = np.array(image)
|
103 |
+
image_array = (image_array - 127.5) / 127.5
|
104 |
+
image_array = np.expand_dims(image_array, axis=0)
|
105 |
+
|
106 |
+
fake_image = generator_model.predict(image_array)
|
107 |
+
fake_image = (fake_image + 1) / 2.0
|
108 |
+
fake_image = np.squeeze(fake_image)
|
109 |
+
|
110 |
+
fake_image = (fake_image * 255).astype(np.uint8)
|
111 |
+
fake_image = Image.fromarray(fake_image)
|
112 |
+
|
113 |
+
img_io = io.BytesIO()
|
114 |
+
fake_image.save(img_io, 'JPEG', quality=70)
|
115 |
+
img_io.seek(0)
|
116 |
+
|
117 |
+
return StreamingResponse(img_io, media_type='image/jpeg')
|
118 |
+
|
119 |
+
if __name__ == '__main__':
|
120 |
+
import uvicorn
|
121 |
+
uvicorn.run(app, host='127.0.0.1', port=8000)
|