Spaces:
Sleeping
Sleeping
tjrtm commited on
Commit ·
d4e4b9f
1
Parent(s): 8ebf188
Simplify Gradio interface to fix schema errors
Browse files- Downgrade to Gradio 4.20.0 which is more stable
- Simplify interface to single button instead of multiple buttons
- Remove complex Row layouts that might cause schema issues
- Add share=True for easier deployment testing
- Streamline function structure to avoid complex parameter handling
Files changed (2)
- app.py+19-47
- requirements.txt+1-1
10 unmodified lines111213141516171819202111 unmodified lines3334353637383940414243444546474821 unmodified lines70717273747576777 unmodified lines85868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013110 unmodified linesprint("Warning: HF_TOKEN not found in environment variables. Image generation may not work.")def wizard_spells():"""Fetches 5 random spells from Wizard World APIReturns:str: A string containing 5 random spells with their names and effects"""try:response = requests.get("https://wizard-world-api.herokuapp.com/Spells")response.raise_for_status()11 unmodified linesreturn f"Error fetching data: {e}"def generate_magical_image(prompt):"""Generate a magical image using a free HuggingFace modelArgs:prompt: Description of the magical image to generateReturns:PIL Image or None: Generated image or None if failed"""try:import requestsfrom PIL import Imageimport io21 unmodified linesprint(f"Error in image generation: {str(e)}")return Nonedef process_spell_request(button_text=""):"""Process spell request and generate visualization"""spells_output = wizard_spells()spell_entries = spells_output.split("\n\n")7 unmodified linesspell_name = lines[0].split("Spell: ")[-1].strip()spell_effect = lines[1].split("Effect: ")[-1].strip()prompt_prefix = "A magical visualization of"if button_text == "Surprise me!":prompt_prefix = "A whimsical and unexpected visualization of"elif button_text == "Show me the magic!":prompt_prefix = "A spectacular and dazzling display of"image_prompt = f"an image of a dark magic wizard executing a magical spell: '{spell_name}', that does: '{spell_effect}', in cyber city and Drum and Bass party"image_result = generate_magical_image(image_prompt)return selected_spell, image_resultreturn "Error processing spell", Nonedef create_gradio_interface():"""Create the Gradio interface"""with gr.Blocks(title="Wizard Spells Generator", theme=gr.themes.Soft()) as interface:gr.Markdown("# 🧙♂️ Magical Spell Visualizer 🪄")gr.Markdown("Click a button to generate a magic spell and visualize it in a generated image!")with gr.Row():surprise_btn = gr.Button("Surprise me!", variant="primary")magic_btn = gr.Button("Show me the magic!", variant="secondary")with gr.Row():spell_text = gr.Textbox(label="Spell Description", lines=3, interactive=False)spell_image = gr.Image(label="Spell Visualization")surprise_btn.click(fn=lambda: process_spell_request("Surprise me!"),outputs=[spell_text, spell_image])magic_btn.click(fn=lambda: process_spell_request("Show me the magic!"),outputs=[spell_text, spell_image])gr.Markdown("### 📜 Each click reveals a new random spell with its magical visualization!")return interfaceif __name__ == "__main__":interface = create_gradio_interface()interface.launch()10 unmodified lines1112131415161711 unmodified lines293031323334353621 unmodified lines58596061626364657 unmodified lines73747576777879808182838485868788899091929394959697989910010110210310 unmodified linesprint("Warning: HF_TOKEN not found in environment variables. Image generation may not work.")def wizard_spells():"""Fetches 5 random spells from Wizard World API"""try:response = requests.get("https://wizard-world-api.herokuapp.com/Spells")response.raise_for_status()11 unmodified linesreturn f"Error fetching data: {e}"def generate_magical_image(prompt):"""Generate a magical image using a free HuggingFace model"""try:from PIL import Imageimport io21 unmodified linesprint(f"Error in image generation: {str(e)}")return Nonedef get_random_spell_with_image():"""Get a random spell and generate an image for it"""spells_output = wizard_spells()spell_entries = spells_output.split("\n\n")7 unmodified linesspell_name = lines[0].split("Spell: ")[-1].strip()spell_effect = lines[1].split("Effect: ")[-1].strip()image_prompt = f"a dark magic wizard executing a magical spell: '{spell_name}', that does: '{spell_effect}', in cyber city and Drum and Bass party"image_result = generate_magical_image(image_prompt)return selected_spell, image_resultreturn "Error processing spell", None# Create simple Gradio interfacedef create_interface():with gr.Blocks(title="Wizard Spells") as demo:gr.Markdown("# 🧙♂️ Magical Spell Visualizer 🪄")gr.Markdown("Click the button to generate a random magic spell with visualization!")generate_btn = gr.Button("Generate Magic Spell!", variant="primary")spell_output = gr.Textbox(label="Spell Description", lines=4)image_output = gr.Image(label="Spell Visualization")generate_btn.click(fn=get_random_spell_with_image,outputs=[spell_output, image_output])return demoif __name__ == "__main__":interface = create_interface()interface.launch(share=True)
1234gradio==4.44.0markdownifyrequestsduckduckgo_search1234gradio==4.20.0markdownifyrequestsduckduckgo_search
app.py+19-47
requirements.txt+1-1