Ifeanyi commited on
Commit
b3399e1
·
verified ·
1 Parent(s): 844a3aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +256 -56
app.py CHANGED
@@ -1,58 +1,258 @@
1
- from google import genai
2
- from google.genai import types
3
- from PIL import Image
4
- from io import BytesIO
5
  import gradio as gr
6
- import base64
7
- import os
8
-
9
- api_key = os.getenv("GEMINI_API_KEY")
10
-
11
- def generate_image(prompt):
12
-
13
- """
14
- Transforms text into an image.
15
-
16
- Args:
17
- prompt: A natural language instruction that is used to generate an image.
18
-
19
- Returns:
20
- An image.
21
- """
22
-
23
- client = genai.Client(api_key=api_key)
24
-
25
- prompt = tuple(prompt)
26
-
27
- response = client.models.generate_content(
28
- model="gemini-2.0-flash-preview-image-generation",
29
- contents=prompt,
30
- config=types
31
- .GenerateContentConfig(
32
- response_modalities=['TEXT', 'IMAGE']
33
- )
34
- )
35
-
36
- for part in response.candidates[0].content.parts:
37
- if part.text is not None:
38
- pass
39
- elif part.inline_data is not None:
40
- image = Image.open(BytesIO((part.inline_data.data)))
41
- # image.save('gemini-native-image.png')
42
- # display(image)
43
- return image
44
-
45
- # build gradio interface
46
- app = gr.Interface(fn = generate_image,
47
- inputs = gr.Text(label="Prompt",placeholder="Type your prompt here. . ."),
48
- outputs = gr.Image(label="Generated Image"),
49
- title="Gemini Image Generator",
50
- examples=["A 3D rendering of a little black girl wearing a colorful dress and smiling broadly at the camera",
51
- "Disney and Pixar-style playful bunny skipping about in a garden full of carrots and lettuce",
52
- "A strong black female superhero with braided hair flying in the bright blue sky",
53
- "A jazz musician playing his saxophone and surrounded by colorful musical notes."])
54
-
55
-
56
- # launch application
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  if __name__ == "__main__":
58
- app.launch(mcp_server = True)
 
1
+ from ImageNet import image2net
2
+ from CV2Net import cv2net
3
+ from Download import save_csv
 
4
  import gradio as gr
5
+
6
+ css = css = """
7
+ .action-btn {
8
+ align-items: center;
9
+ appearance: none;
10
+ background-image: radial-gradient(100% 100% at 100% 0, #5aff76 0, #07911a 100%);
11
+ border: 0;
12
+ border-radius: 6px;
13
+ box-shadow: rgba(45, 35, 66, .4) 0 2px 4px,rgba(45, 35, 66, .3) 0 7px 13px -3px,rgba(58, 65, 111, .5) 0 -3px 0 inset;
14
+ box-sizing: border-box;
15
+ color: #0c0b0b;
16
+ cursor: pointer;
17
+ display: inline-flex;
18
+ font-family: "JetBrains Mono",monospace;
19
+ height: 48px;
20
+ justify-content: center;
21
+ line-height: 1;
22
+ list-style: none;
23
+ overflow: hidden;
24
+ padding-left: 16px;
25
+ padding-right: 16px;
26
+ position: relative;
27
+ text-align: left;
28
+ text-decoration: none;
29
+ transition: box-shadow .15s,transform .15s;
30
+ user-select: none;
31
+ -webkit-user-select: none;
32
+ touch-action: manipulation;
33
+ white-space: nowrap;
34
+ will-change: box-shadow,transform;
35
+ font-size: 18px;
36
+ }
37
+
38
+ .action-btn:focus {
39
+ outline: none;
40
+ box-shadow: none
41
+ }
42
+
43
+ .action-btn:hover {
44
+ box-shadow: rgba(45, 35, 66, .4) 0 4px 8px, rgba(45, 35, 66, .3) 0 7px 13px -3px, #3ce065 0 -3px 0 inset;
45
+ transform: translateY(-2px);
46
+ }
47
+
48
+ .action-btn:active {
49
+ box-shadow: #3ce065 0 3px 7px inset;
50
+ transform: translateY(2px);
51
+ }
52
+
53
+ /* Enhanced dark theme for dataframe - Multiple selectors for compatibility */
54
+ #dark-df,
55
+ #dark-df .MuiDataGrid-root,
56
+ #dark-df .dataframe,
57
+ #dark-df .gr-dataframe,
58
+ #dark-df table {
59
+ background-color: #0a0a0a !important;
60
+ color: #e8e8e8 !important;
61
+ border: 2px solid #404040 !important;
62
+ border-radius: 8px !important;
63
+ font-size: 14px;
64
+ }
65
+
66
+ /* Headers for both MUI and standard tables */
67
+ #dark-df .MuiDataGrid-columnHeaders,
68
+ #dark-df thead,
69
+ #dark-df th {
70
+ background-color: #1a1a1a !important;
71
+ color: #ffffff !important;
72
+ border-bottom: 2px solid #505050 !important;
73
+ font-weight: 600 !important;
74
+ }
75
+
76
+ #dark-df .MuiDataGrid-columnHeader {
77
+ border-right: 1px solid #404040 !important;
78
+ }
79
+
80
+ /* Cells for both MUI and standard tables */
81
+ #dark-df .MuiDataGrid-cell,
82
+ #dark-df td {
83
+ background-color: #0a0a0a !important;
84
+ color: #e8e8e8 !important;
85
+ border-bottom: 1px solid #333333 !important;
86
+ border-right: 1px solid #333333 !important;
87
+ padding: 8px 12px !important;
88
+ }
89
+
90
+ #dark-df .MuiDataGrid-cell:last-of-type {
91
+ border-right: none !important;
92
+ }
93
+
94
+ /* Rows for both MUI and standard tables */
95
+ #dark-df .MuiDataGrid-row,
96
+ #dark-df tr {
97
+ background-color: #0a0a0a !important;
98
+ }
99
+
100
+ #dark-df .MuiDataGrid-row:nth-of-type(even),
101
+ #dark-df tr:nth-of-type(even) {
102
+ background-color: #111111 !important;
103
+ }
104
+
105
+ #dark-df .MuiDataGrid-row:hover,
106
+ #dark-df tr:hover {
107
+ background-color: #1f1f1f !important;
108
+ }
109
+
110
+ #dark-df .MuiDataGrid-row:nth-of-type(even):hover,
111
+ #dark-df tr:nth-of-type(even):hover {
112
+ background-color: #1f1f1f !important;
113
+ }
114
+
115
+ #dark-df .MuiDataGrid-footerContainer {
116
+ background-color: #1a1a1a !important;
117
+ color: #cccccc !important;
118
+ border-top: 2px solid #505050 !important;
119
+ }
120
+
121
+ /* Add Gradio-specific dataframe styling */
122
+ #dark-df .gr-dataframe,
123
+ #dark-df .gr-dataframe table,
124
+ #dark-df .gr-dataframe tbody,
125
+ #dark-df .gr-dataframe thead {
126
+ background-color: #0a0a0a !important;
127
+ color: #e8e8e8 !important;
128
+ }
129
+
130
+ #dark-df .gr-dataframe th {
131
+ background-color: #1a1a1a !important;
132
+ color: #ffffff !important;
133
+ border: 1px solid #404040 !important;
134
+ }
135
+
136
+ #dark-df .gr-dataframe td {
137
+ background-color: #0a0a0a !important;
138
+ color: #e8e8e8 !important;
139
+ border: 1px solid #333333 !important;
140
+ }
141
+
142
+ #dark-df .gr-dataframe tr:nth-child(even) td {
143
+ background-color: #111111 !important;
144
+ }
145
+
146
+ #dark-df .gr-dataframe tr:hover td {
147
+ background-color: #1f1f1f !important;
148
+ }
149
+
150
+ /* Force override any default styling */
151
+ #dark-df * {
152
+ box-sizing: border-box;
153
+ }
154
+
155
+ #dark-df .gr-dataframe .gr-button {
156
+ background-color: #333333 !important;
157
+ color: #e8e8e8 !important;
158
+ border: 1px solid #555555 !important;
159
+ }
160
+
161
+ #dark-df .gr-dataframe .gr-button:hover {
162
+ background-color: #444444 !important;
163
+ }
164
+ #dark-df .MuiDataGrid-virtualScroller::-webkit-scrollbar {
165
+ width: 8px;
166
+ height: 8px;
167
+ }
168
+
169
+ #dark-df .MuiDataGrid-virtualScroller::-webkit-scrollbar-track {
170
+ background: #1a1a1a;
171
+ }
172
+
173
+ #dark-df .MuiDataGrid-virtualScroller::-webkit-scrollbar-thumb {
174
+ background: #404040;
175
+ border-radius: 4px;
176
+ }
177
+
178
+ #dark-df .MuiDataGrid-virtualScroller::-webkit-scrollbar-thumb:hover {
179
+ background: #505050;
180
+ }
181
+
182
+ /* Selection styling */
183
+ #dark-df .MuiDataGrid-row.Mui-selected {
184
+ background-color: #2d4a3e !important;
185
+ }
186
+
187
+ #dark-df .MuiDataGrid-row.Mui-selected:hover {
188
+ background-color: #365242 !important;
189
+ }
190
+
191
+ #dark-df .MuiDataGrid-cell.Mui-selected {
192
+ background-color: #2d4a3e !important;
193
+ }
194
+
195
+ /* Sorting and filtering icons */
196
+ #dark-df .MuiDataGrid-iconSeparator {
197
+ color: #666666 !important;
198
+ }
199
+
200
+ #dark-df .MuiDataGrid-sortIcon {
201
+ color: #cccccc !important;
202
+ }
203
+
204
+ #dark-df .MuiDataGrid-filterIcon {
205
+ color: #cccccc !important;
206
+ }
207
+ """
208
+ with gr.Blocks(
209
+ theme=gr.themes.Base(primary_hue="teal", secondary_hue="amber").set(
210
+ body_background_fill="*neutral_950",
211
+ body_text_color="*neutral_200",
212
+ background_fill_primary="*neutral_900",
213
+ background_fill_secondary="*neutral_800",
214
+ border_color_primary="*neutral_700",
215
+ block_background_fill="*neutral_900",
216
+ input_background_fill="*neutral_800",
217
+ button_primary_background_fill="*primary_600",
218
+ button_primary_background_fill_hover="*primary_700",
219
+ button_secondary_background_fill="*neutral_700",
220
+ button_secondary_background_fill_hover="*neutral_600"
221
+ ),
222
+ css=css,
223
+ ) as app:
224
+ gr.HTML("<h1 style='text-align:center;color:#07911a;font-weight:bold;'>🖼️ Image To Network 🕸️</h1>")
225
+ with gr.Column(elem_classes="cols"):
226
+ with gr.Row():
227
+ gr.Markdown("""
228
+ How To Use:
229
+ - Visit [Google AI Studio](https://aistudio.google.com/app/apikey) and obtain your Google API key.
230
+ - Paste your Google API key
231
+ - Upload image files from local folder
232
+ - Generate network data from uploaded images
233
+ - Download data for analysis in NodeXL
234
+ """)
235
+ with gr.Row():
236
+ api_key = gr.Textbox(label="🔑 Google API Key",placeholder="Paste your Google API Key",elem_classes="widgets")
237
+ with gr.Row():
238
+ image_input = gr.File(file_types=["image"],
239
+ label="🖼️ Upload Image Files",
240
+ file_count="multiple",
241
+ elem_classes="widgets",
242
+ interactive=True)
243
+ df_output = gr.Dataframe(label="🕸️ Network Data", elem_id="dark-df")
244
+ with gr.Column(elem_classes="cols"):
245
+ with gr.Row():
246
+ btn = gr.Button("🧬 Generate Data",elem_classes="action-btn", elem_id = "click")
247
+ download_btn = gr.DownloadButton("⬇️ Download Data", elem_classes="action-btn")
248
+ with gr.Column():
249
+ with gr.Row():
250
+ clear = gr.Button("🗑️ Clear",value="", elem_classes="action-btn")
251
+
252
+
253
+ btn.click(image2net, inputs=[image_input,api_key], outputs=[df_output, download_btn])
254
+ clear.click(lambda: [None,None,None], inputs = [], outputs = [image_input, df_output])
255
+
256
+
257
  if __name__ == "__main__":
258
+ app.launch()