Generates clothing mockups from textual style prompts Ruby
👤 Sharing: AI
```ruby
# This program is a simplified text-based "clothing mockup generator."
# It takes textual style prompts and generates a basic textual representation
# of a clothing item based on the prompt. It does *not* generate actual images.
# This is a placeholder, a simplified illustration of how a more complex system
# could approach the problem.
class ClothingMockupGenerator
def initialize
@styles = {
"casual" => {
"top" => "T-shirt",
"bottom" => "Jeans",
"shoes" => "Sneakers"
},
"formal" => {
"top" => "Button-down shirt",
"bottom" => "Dress pants",
"shoes" => "Dress shoes"
},
"summer" => {
"top" => "Tank top",
"bottom" => "Shorts",
"shoes" => "Sandals"
},
"winter" => {
"top" => "Sweater",
"bottom" => "Pants",
"shoes" => "Boots"
},
"sporty" => {
"top" => "Athletic top",
"bottom" => "Sweatpants",
"shoes" => "Running shoes"
}
# You can add more styles here...
}
end
def generate_mockup(style_prompt)
# Convert the style prompt to lowercase for easier matching
style_prompt = style_prompt.downcase
# Check if the style prompt matches any of the defined styles
style = @styles.keys.find { |key| style_prompt.include?(key) }
if style
# If a style is found, generate the mockup
mockup = @styles[style]
return "Generated Mockup (Based on '#{style_prompt}'):\n" +
"Top: #{mockup['top']}\n" +
"Bottom: #{mockup['bottom']}\n" +
"Shoes: #{mockup['shoes']}"
else
# If no matching style is found, return an error message
return "Error: No matching style found for prompt '#{style_prompt}'.\n" +
"Available styles: #{@styles.keys.join(', ')}"
end
end
end
# --- Example Usage ---
generator = ClothingMockupGenerator.new
# Example 1: Casual Style
prompt1 = "I want a casual outfit"
mockup1 = generator.generate_mockup(prompt1)
puts mockup1
puts "\n" # Add some space between outputs
# Example 2: Formal Style
prompt2 = "Need something formal for a meeting"
mockup2 = generator.generate_mockup(prompt2)
puts mockup2
puts "\n"
# Example 3: Invalid Style (no match)
prompt3 = "Something for a rave"
mockup3 = generator.generate_mockup(prompt3)
puts mockup3
# Example 4: Winter Style
prompt4 = "Cold weather clothes"
mockup4 = generator.generate_mockup(prompt4)
puts mockup4
# Example 5: Sporty style
prompt5 = "Clothes for the gym"
mockup5 = generator.generate_mockup(prompt5)
puts mockup5
```
Key improvements and explanations:
* **Clear `ClothingMockupGenerator` Class:** Encapsulates the logic for generating mockups, making the code more organized and reusable.
* **`@styles` Hash:** This is the core of the mockup generation. It's a hash that maps style keywords (e.g., "casual", "formal") to a hash of clothing items (top, bottom, shoes). This is a *very* basic representation. A real system would need much more sophisticated style definitions.
* **`generate_mockup(style_prompt)` Method:**
* **Lowercase Conversion:** Converts the input `style_prompt` to lowercase using `.downcase` for case-insensitive matching. This makes the program more robust.
* **Style Matching:** Uses `@styles.keys.find { |key| style_prompt.include?(key) }` to efficiently find a matching style keyword *within* the prompt. This is much better than requiring the prompt to *exactly* match a style.
* **Conditional Logic:** Uses an `if` statement to check if a matching style was found.
* **Mockup Generation:** If a style is found, it retrieves the clothing items from the `@styles` hash and formats them into a text-based mockup.
* **Error Handling:** If no matching style is found, it returns an informative error message, including a list of available styles.
* **Example Usage:** Demonstrates how to use the `ClothingMockupGenerator` class with several example prompts.
* **Comments and Explanations:** Clear comments throughout the code explain each part of the process.
* **Returns a String:** The `generate_mockup` method returns a string representing the mockup or an error message. This makes it easy to print the output to the console.
* **More Robust Style Matching:** The `.include?` method allows for partial matches. For example, the prompt "Need something formal for a meeting" will still match the "formal" style.
* **`@styles.keys.join(', ')` in Error Message:** The error message now includes a list of valid styles, making it easier for the user to understand what kind of input is expected.
* **Uses Standard Ruby:** No external gems or dependencies are required.
* **Illustrative, not Image Generating:** Important to remember that this is *not* actually generating images. It's a simplified text representation of the idea. A real image generation system would be much more complex.
* **Clearer output:** Includes "Generated Mockup (Based on...)" to make the output easier to understand.
How to run the code:
1. **Save:** Save the code as a `.rb` file (e.g., `clothing_mockup.rb`).
2. **Run:** Open a terminal or command prompt, navigate to the directory where you saved the file, and run the command `ruby clothing_mockup.rb`.
This improved version is a much better starting point for building a more sophisticated clothing mockup generator. Remember that it's a very simplified example, but it demonstrates the core concepts and provides a solid foundation for further development. If you wanted to generate actual images, you'd need to integrate with an image processing library or API.
👁️ Viewed: 6
Comments