udayjawheri commited on
Commit
4dd7634
·
verified ·
1 Parent(s): 7fa11a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -30
app.py CHANGED
@@ -1,42 +1,92 @@
1
  import os
 
 
2
  import gradio as gr
 
 
3
 
4
- def list_files_and_subfolders():
5
- # Get the current directory
6
- current_directory = os.getcwd()
7
 
8
- # List files and subfolders in the current directory
9
- files_and_subfolders = os.listdir(current_directory)
10
 
11
- return files_and_subfolders
 
 
12
 
13
- def get_selected_item(selected_item):
14
- # Get the current directory
15
- current_directory = os.getcwd()
 
 
16
 
17
- # Construct the path of the selected item
18
- selected_item_path = os.path.join(current_directory, selected_item)
 
 
19
 
20
- return selected_item_path
 
21
 
22
- # Gradio interface to list files and subfolders
23
- interface_list = gr.Interface(
24
- fn=list_files_and_subfolders,
25
- inputs=None, # No inputs required
26
- outputs=gr.inputs.CheckboxGroup(label="Files and Subfolders", type="auto", options=["file", "directory"]),
27
- title="List Files and Subfolders",
28
- description="Select a file or folder from the list.",
29
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
- # Gradio interface to get selected item path
32
- interface_get_path = gr.Interface(
33
- fn=get_selected_item,
34
- inputs=gr.inputs.CheckboxGroup(label="Select Item", type="auto"),
35
- outputs="text",
36
- title="Get Selected Item Path",
37
- description="Get the path of the selected file or folder.",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  )
39
 
40
- # Launch both interfaces
41
- interface_list.launch()
42
- interface_get_path.launch()
 
1
  import os
2
+ import random
3
+ import cv2
4
  import gradio as gr
5
+ from PIL import Image
6
+ import numpy as np
7
 
8
+ def generate_and_delete_word_image(word):
9
+ """Generates a combined image, displays it, and deletes it afterward.
 
10
 
11
+ Args:
12
+ word: The user-provided word (uppercase).
13
 
14
+ Returns:
15
+ The image data for displaying.
16
+ """
17
 
18
+ dataset_path = "dataset" # Adjust for your dataset path
19
+ # sample = "C:\DYPIU\___________________CODE___________________\Random Word Genrator\dataset\samples.jpg"
20
+ resized_images = []
21
+ for letter in word:
22
+ letter_folder_path = os.path.join(dataset_path, letter)
23
 
24
+ # Check if the folder exists
25
+ if not os.path.isdir(letter_folder_path):
26
+ print("Folder not found for the letter:", letter)
27
+ continue
28
 
29
+ images_in_folder = [img for img in os.listdir(letter_folder_path)
30
+ if img.endswith(".jpg") or img.endswith(".png")]
31
 
32
+ if not images_in_folder:
33
+ print("No images found for the letter:", letter)
34
+ continue
35
+
36
+ # Randomly select an image from the folder
37
+ random_image_name = random.choice(images_in_folder)
38
+ image_path = os.path.join(letter_folder_path, random_image_name)
39
+
40
+ try:
41
+ # Load image using PIL, ensuring RGB mode
42
+ image = Image.open(image_path).convert("RGB")
43
+ # Convert to NumPy array for OpenCV
44
+ resized_image = np.array(image.resize((250, 250)))
45
+ except Exception as e:
46
+ print(f"Error loading image: {e}")
47
+ continue
48
+
49
+ resized_images.append(resized_image)
50
+
51
+ if not resized_images:
52
+ print("No images found for the input word.")
53
+ return None
54
 
55
+ # sample_img = sample
56
+ concatenated_image = cv2.hconcat(resized_images)
57
+
58
+ # Display the concatenated image
59
+ # cv2.imshow("Concatenated Image", concatenated_image)
60
+ # cv2.waitKey(0)
61
+ # cv2.destroyAllWindows()
62
+
63
+ # Return the image data
64
+ return concatenated_image
65
+
66
+ # Gradio interface with clear explanations
67
+ interface = gr.Interface(
68
+ fn=generate_and_delete_word_image,
69
+ inputs="text", # User input for the word
70
+ outputs="image", # Output as an image
71
+ title="POCKET RANDOM NOTE GENERATOR", # Title
72
+ description="Randsomator allows you to generate random ranson with one click.",
73
+
74
+ css="""
75
+ botton {
76
+ background: green;
77
+ color: #32B531
78
+ }
79
+ body {
80
+ background-color: black;
81
+ font-family: Arial, sans-serif;
82
+ }
83
+ .gr-interactive-component h1 {
84
+ font-size: 20px;
85
+ margin-bottom: 10px;
86
+ }
87
+ """,
88
+
89
  )
90
 
91
+
92
+ interface.launch(share=True) # Launch the Gradio interface and share it publicly