rockonrover commited on
Commit
54e8836
·
verified ·
1 Parent(s): 24a7a59

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +177 -0
app.py ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ pip install pillow gradio requests
2
+ import requests
3
+ from PIL import Image
4
+ from io import BytesIO
5
+ import gradio as gr
6
+ import base64
7
+
8
+ # Optimized function to call the Hugging Face API and convert JSON to an image
9
+ def generate_image_from_text(prompt):
10
+ try:
11
+ # Use the FLUX model endpoint and your Hugging Face API key
12
+ API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
13
+ headers = {"Authorization": f"Bearer {os.getenv('HF_API_KEY')}"}
14
+
15
+ # Send the text prompt to the Hugging Face API
16
+ response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
17
+ response.raise_for_status() # Raise an error for bad HTTP responses
18
+
19
+ # Process the response efficiently
20
+ content_type = response.headers.get("content-type")
21
+ if content_type == "application/json":
22
+ response_json = response.json()
23
+ # Check for base64-encoded image or image URL
24
+ image_data = response_json.get("generated_image")
25
+ image_url = response_json.get("generated_image_url")
26
+
27
+ if image_data: # Base64-encoded image
28
+ image_bytes = BytesIO(base64.b64decode(image_data))
29
+ elif image_url: # Image URL
30
+ image_response = requests.get(image_url)
31
+ image_response.raise_for_status()
32
+ image_bytes = BytesIO(image_response.content)
33
+ else:
34
+ return "Error: No image data found in the response."
35
+
36
+ else: # Binary image directly in response
37
+ image_bytes = BytesIO(response.content)
38
+
39
+ # Load the image once for faster return
40
+ return Image.open(image_bytes)
41
+
42
+ except requests.exceptions.RequestException as req_err:
43
+ return f"Error with the request: {req_err}"
44
+ except Exception as e:
45
+ return f"Error generating image: {e}"
46
+
47
+ # Gradio interface for user-friendly interaction
48
+ iface = gr.Interface(
49
+ fn=generate_image_from_text,
50
+ inputs=gr.Textbox(
51
+ label="Enter a prompt for the image",
52
+ placeholder="e.g., A beautiful futuristic city",
53
+ lines=4, # Make the textbox bigger
54
+ max_length=200, # Set a maximum character length
55
+ interactive=True, # Allow for dynamic text change
56
+ elem_id="prompt-box" # ID for CSS targeting
57
+ ),
58
+ outputs="image",
59
+ title=" Text-to-Image Generator",
60
+ description="Here my portfolio : maazirfan.netlify.app",
61
+ allow_flagging="never", # Hide the flag button for a cleaner UI
62
+ css="""
63
+ body {
64
+ background-color: #1d1d1d; /* Dark background */
65
+ color: white;
66
+ font-family: 'Arial', sans-serif;
67
+ }
68
+
69
+ .gradio-container {
70
+ background-color: #2d2d2d; /* Slightly lighter background */
71
+ padding: 20px;
72
+ border-radius: 15px;
73
+ max-width: 800px;
74
+ margin: auto;
75
+ box-shadow: 0 0 10px rgba(0, 0, 0, 0.5); /* Soft shadow for depth */
76
+ display: flex;
77
+ flex-direction: column;
78
+ align-items: center;
79
+ }
80
+
81
+ .gradio-title {
82
+ font-size: 36px;
83
+ font-weight: bold;
84
+ text-align: center;
85
+ color: #4a90e2; /* Light blue for title */
86
+ margin-bottom: 20px;
87
+ }
88
+
89
+ .gradio-description {
90
+ font-size: 18px;
91
+ text-align: center;
92
+ color: #aaa; /* Light grey for description */
93
+ margin-bottom: 30px;
94
+ }
95
+
96
+ #prompt-box {
97
+ font-size: 18px;
98
+ background-color: #333;
99
+ border: 2px solid #444;
100
+ border-radius: 8px;
101
+ padding: 12px;
102
+ color: white;
103
+ width: 100%;
104
+ margin-bottom: 20px;
105
+ }
106
+
107
+ .gr-button {
108
+ background-color: #4a90e2; /* Light blue button */
109
+ color: white;
110
+ font-size: 16px;
111
+ border-radius: 8px;
112
+ border: none;
113
+ padding: 10px;
114
+ width: 200px;
115
+ margin-top: 20px;
116
+ cursor: pointer;
117
+ text-align: center;
118
+ }
119
+
120
+ .gr-button:hover {
121
+ background-color: #357ABD; /* Darker blue on hover */
122
+ }
123
+
124
+ .gr-image {
125
+ border-radius: 12px;
126
+ border: 2px solid #444;
127
+ margin-top: 20px;
128
+ max-width: 100%; /* Ensure image is responsive */
129
+ }
130
+
131
+ /* Custom CSS for Multicolor Spinner */
132
+ .gradio-spinner {
133
+ border: 4px solid transparent;
134
+ border-top: 4px solid #4a90e2;
135
+ border-radius: 50%;
136
+ width: 50px;
137
+ height: 50px;
138
+ animation: spin 1s linear infinite, color-change 1.5s ease-in-out infinite;
139
+ }
140
+
141
+ @keyframes spin {
142
+ 0% { transform: rotate(0deg); }
143
+ 100% { transform: rotate(360deg); }
144
+ }
145
+
146
+ @keyframes color-change {
147
+ 0% { border-top-color: #4a90e2; }
148
+ 25% { border-top-color: #e91e63; }
149
+ 50% { border-top-color: #ffeb3b; }
150
+ 75% { border-top-color: #4caf50; }
151
+ 100% { border-top-color: #4a90e2; }
152
+ }
153
+
154
+ /* Style the div containing the image */
155
+ .gr-image-container {
156
+ width: 50vw;
157
+ height: 50vh;
158
+ overflow: hidden;
159
+ display: flex;
160
+ justify-content: center;
161
+ align-items: center;
162
+ background-color: #333; /* Dark background to highlight image */
163
+ border-radius: 12px;
164
+ margin-top: 20px;
165
+ }
166
+
167
+ .gr-image-container img {
168
+ max-width: 100%;
169
+ max-height: 100%;
170
+ object-fit: contain;
171
+ }
172
+
173
+ """,
174
+ )
175
+
176
+ # Launch the Gradio app
177
+ iface.launch()