Spaces:
Build error
Build error
Download ui.py from sp103107/growing-guide: direct link, hf CLI and curl.
- Browser
- Download file 7.51 kB
-
https://huggingface.co/spaces/sp103107/growing-guide/resolve/main/ui.py
- Command line
-
hf download hf://spaces/sp103107/growing-guide/ui.py
-
curl -L -o ui.py https://huggingface.co/spaces/sp103107/growing-guide/resolve/main/ui.py
7.51 kB
| from typing import Dict, Optional | |
| import gradio as gr | |
| from .guide_manager import GrowingGuideManager | |
| class GrowingGuideUI: | |
| def __init__(self): | |
| self.guide_manager = GrowingGuideManager() | |
| self.theme = gr.themes.Soft( | |
| primary_hue="green", | |
| secondary_hue="gray", | |
| font=["Quicksand", "ui-sans-serif"] | |
| ) | |
| def create_strain_card(self, strain_data: Dict) -> str: | |
| """Create a formatted strain card HTML""" | |
| return f""" | |
| <div class="strain-info"> | |
| <h2 class="strain-title">{strain_data['name']}</h2> | |
| <div class="profile-section"> | |
| <span class="strain-type {strain_data['type'].lower()}">{strain_data['type']}</span> | |
| <span class="thc">THC: {strain_data.get('thc', {}).get('min')}% - {strain_data.get('thc', {}).get('max')}%</span> | |
| </div> | |
| <div class="effects-section"> | |
| <h3>Effects</h3> | |
| <p class="effects">{', '.join(strain_data.get('effects', []))}</p> | |
| </div> | |
| <div class="medical-section"> | |
| <h3>Medical Benefits</h3> | |
| <p class="medical">{', '.join(strain_data.get('medical', []))}</p> | |
| </div> | |
| {self._create_grow_guide_section(strain_data) if strain_data.get('grow_guide') else ''} | |
| </div> | |
| """ | |
| def _create_grow_guide_section(self, strain_data: Dict) -> str: | |
| """Create the growing guide section HTML""" | |
| guide = strain_data.get('grow_guide', {}) | |
| return f""" | |
| <div class="growing-section"> | |
| <h3>Growing Guide</h3> | |
| <div class="grow-details"> | |
| <p><strong>Difficulty:</strong> {guide.get('difficulty', 'N/A')}</p> | |
| <p><strong>Flowering Time:</strong> {guide.get('stages', {}).get('flowering', {}).get('duration', 'N/A')}</p> | |
| <p><strong>Environment:</strong> {', '.join(guide.get('grow_environment', {}).get('suitable_for', []))}</p> | |
| </div> | |
| <div class="training-methods"> | |
| <p><strong>Training Techniques:</strong></p> | |
| <ul> | |
| {self._create_list_items(guide.get('training_techniques', {}).get('recommended', []))} | |
| </ul> | |
| </div> | |
| </div> | |
| """ | |
| def _create_list_items(self, items: list) -> str: | |
| return ''.join([f"<li>{item}</li>" for item in items]) | |
| def create_tabs(self) -> gr.Tabs: | |
| """Create the main interface tabs""" | |
| with gr.Tabs() as tabs: | |
| with gr.Tab("Strain Search", id="search"): | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| search_input = gr.Textbox( | |
| label="Search Strains", | |
| placeholder="Enter strain name or effect..." | |
| ) | |
| with gr.Row(): | |
| strain_type = gr.Dropdown( | |
| choices=["All", "Indica", "Sativa", "Hybrid"], | |
| label="Type", | |
| value="All" | |
| ) | |
| has_guide = gr.Checkbox( | |
| label="Has Growing Guide", | |
| value=False | |
| ) | |
| search_button = gr.Button("Search", variant="primary") | |
| with gr.Column(scale=2): | |
| results = gr.HTML(label="Results") | |
| with gr.Tab("Growing Guide", id="guide"): | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| strain_input = gr.Textbox( | |
| label="Strain Name", | |
| placeholder="Enter exact strain name..." | |
| ) | |
| guide_button = gr.Button("Get Guide", variant="primary") | |
| with gr.Column(scale=2): | |
| guide_output = gr.HTML(label="Growing Guide") | |
| # Set up event handlers | |
| search_button.click( | |
| fn=self.search_strains, | |
| inputs=[search_input, strain_type, has_guide], | |
| outputs=results | |
| ) | |
| guide_button.click( | |
| fn=self.get_grow_guide, | |
| inputs=strain_input, | |
| outputs=guide_output | |
| ) | |
| return tabs | |
| def search_strains(self, query: str, strain_type: str, has_guide: bool) -> str: | |
| """Search strains and return formatted HTML results""" | |
| try: | |
| results = self.guide_manager.search_strains(query, strain_type, has_guide) | |
| if not results: | |
| return "<div class='no-results'>No strains found matching your criteria.</div>" | |
| return "".join([self.create_strain_card(strain) for strain in results]) | |
| except Exception as e: | |
| return f"<div class='error'>Error searching strains: {str(e)}</div>" | |
| def get_grow_guide(self, strain_name: str) -> str: | |
| """Get and format growing guide for a strain""" | |
| try: | |
| guide = self.guide_manager.get_grow_guide(strain_name) | |
| if not guide: | |
| return "<div class='no-guide'>No growing guide found for this strain.</div>" | |
| strain_data = {"name": strain_name, "grow_guide": guide} | |
| return self.create_strain_card(strain_data) | |
| except Exception as e: | |
| return f"<div class='error'>Error fetching grow guide: {str(e)}</div>" | |
| def get_custom_css(self) -> str: | |
| """Get custom CSS for the interface""" | |
| return """ | |
| /* Strain Card Styling */ | |
| .strain-info { | |
| background: white; | |
| border-radius: 12px; | |
| padding: 24px; | |
| margin: 16px 0; | |
| box-shadow: 0 2px 8px rgba(0,0,0,0.1); | |
| } | |
| .strain-title { | |
| color: #2E7D32; | |
| font-size: 24px; | |
| margin-bottom: 16px; | |
| } | |
| .strain-type { | |
| padding: 4px 12px; | |
| border-radius: 16px; | |
| font-weight: 500; | |
| margin-right: 12px; | |
| } | |
| .indica { background: #E8F5E9; color: #2E7D32; } | |
| .sativa { background: #FFF3E0; color: #EF6C00; } | |
| .hybrid { background: #E3F2FD; color: #1976D2; } | |
| /* Section Styling */ | |
| .effects-section, .medical-section, .growing-section { | |
| margin: 16px 0; | |
| padding: 16px; | |
| border-radius: 8px; | |
| background: #F8F9FA; | |
| } | |
| h3 { | |
| color: #2E7D32; | |
| margin-bottom: 8px; | |
| } | |
| /* List Styling */ | |
| ul { | |
| list-style-type: none; | |
| padding-left: 0; | |
| } | |
| li { | |
| margin: 4px 0; | |
| padding-left: 20px; | |
| position: relative; | |
| } | |
| li:before { | |
| content: "•"; | |
| color: #2E7D32; | |
| position: absolute; | |
| left: 0; | |
| } | |
| /* Messages */ | |
| .error { | |
| color: #D32F2F; | |
| padding: 16px; | |
| background: #FFEBEE; | |
| border-radius: 8px; | |
| } | |
| .no-results, .no-guide { | |
| color: #666; | |
| padding: 16px; | |
| background: #F5F5F5; | |
| border-radius: 8px; | |
| text-align: center; | |
| } | |
| """ |