Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,193 +1,50 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
""
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
}
|
| 25 |
-
|
| 26 |
-
complexity = next(value for (low, high), value in complexity_levels.items() if low <= age <= high)
|
| 27 |
-
|
| 28 |
-
# Enhanced prompts for better coloring pages
|
| 29 |
-
passover_pages = {
|
| 30 |
-
"simple": [
|
| 31 |
-
"Line drawing of a matzah for a simple coloring page, thick black outlines, white background, perfect for a child's coloring book",
|
| 32 |
-
"Simple black and white outline of a Seder plate with clear divisions, coloring book style, thick lines",
|
| 33 |
-
"Black and white line art of baby Moses in a basket for a child's coloring book, simple outlines",
|
| 34 |
-
"Simple black and white outline of a frog (plague) for children's coloring book, thick lines, no shading"
|
| 35 |
-
],
|
| 36 |
-
"basic": [
|
| 37 |
-
"Black and white outline of the Parting of the Red Sea scene for a coloring book, clear distinct lines",
|
| 38 |
-
"Coloring book page with Ten Plagues illustrations, black outline on white background, no shading",
|
| 39 |
-
"Black and white outline illustrations of the Four Questions for a Passover coloring book",
|
| 40 |
-
"Afikoman hunt maze in black and white outline for a coloring book, thick lines"
|
| 41 |
-
],
|
| 42 |
-
"intermediate": [
|
| 43 |
-
"Detailed black and white outline of a Seder table for a coloring book, clear distinct elements",
|
| 44 |
-
"Black and white outline of Exodus story scenes for coloring book, clear lines",
|
| 45 |
-
"Hebrew letter decorations for 'Pesach' in black and white outline style for coloring book",
|
| 46 |
-
"Black and white outline of Egyptian pyramids with Israelites for a coloring book"
|
| 47 |
-
],
|
| 48 |
-
"advanced": [
|
| 49 |
-
"Intricate black and white line art of Haggadah illustrations for a coloring book, detailed outlines",
|
| 50 |
-
"Detailed symbolism scene in black and white outline for an advanced coloring book",
|
| 51 |
-
"Combined Passover story panels in black and white line art for coloring book",
|
| 52 |
-
"Hebrew calligraphy art in black and white outline for a coloring book, intricate details"
|
| 53 |
-
]
|
| 54 |
-
}
|
| 55 |
|
| 56 |
-
|
| 57 |
-
descriptions = [f"- {page.split(',')[0]}" for page in selected_pages]
|
| 58 |
-
|
| 59 |
-
# Try to generate images with improved prompts
|
| 60 |
try:
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
# Use more specific prompts for coloring pages
|
| 64 |
-
enhanced_prompts = []
|
| 65 |
-
for page in selected_pages:
|
| 66 |
-
enhanced_prompts.append(f"{page}, suitable for printing as a coloring page")
|
| 67 |
-
|
| 68 |
-
images = [image_generation_tool(prompt) for prompt in enhanced_prompts]
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
# Create a temporary file with a .png extension
|
| 74 |
-
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as temp_file:
|
| 75 |
-
temp_path = temp_file.name
|
| 76 |
-
img.save(temp_path)
|
| 77 |
-
image_paths.append(temp_path)
|
| 78 |
-
|
| 79 |
-
# Create a formatted response with descriptions
|
| 80 |
-
response = f"## Passover Coloring Pages for a {age}-year-old:\n\n"
|
| 81 |
-
response += "\n".join(descriptions)
|
| 82 |
-
|
| 83 |
-
# Return the response with image paths
|
| 84 |
-
return f"{response}\n\nImages are saved and ready for display."
|
| 85 |
-
except Exception as e:
|
| 86 |
-
# If image generation fails, return text description
|
| 87 |
-
response = f"## Passover Coloring Pages for a {age}-year-old:\n\n"
|
| 88 |
-
response += "\n".join(descriptions)
|
| 89 |
-
return f"{response}\n\nError generating images: {str(e)}"
|
| 90 |
-
|
| 91 |
-
def create_gradio_interface():
|
| 92 |
-
import gradio as gr
|
| 93 |
-
|
| 94 |
-
# Create a custom interface that can display images
|
| 95 |
-
with gr.Blocks() as demo:
|
| 96 |
-
with gr.Row():
|
| 97 |
-
with gr.Column(scale=1):
|
| 98 |
-
gr.Markdown("# Passover Coloring Pages Generator")
|
| 99 |
-
|
| 100 |
-
with gr.Row():
|
| 101 |
-
with gr.Column(scale=1):
|
| 102 |
-
input_box = gr.Number(label="Enter child's age (1-12)", minimum=1, maximum=12, step=1)
|
| 103 |
-
submit_btn = gr.Button("Generate Coloring Pages")
|
| 104 |
-
|
| 105 |
-
with gr.Row():
|
| 106 |
-
with gr.Column(scale=1):
|
| 107 |
-
output_text = gr.Markdown(label="Generated Pages")
|
| 108 |
-
|
| 109 |
-
with gr.Row():
|
| 110 |
-
gallery = gr.Gallery(label="Coloring Pages")
|
| 111 |
-
|
| 112 |
-
def process_input(age):
|
| 113 |
try:
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
return "Please enter a valid age between 1 and 12.", []
|
| 117 |
-
|
| 118 |
-
# Generate the coloring pages
|
| 119 |
-
result = generate_passover_coloring_pages(int(age))
|
| 120 |
-
|
| 121 |
-
# Get the image paths from the temporary files
|
| 122 |
-
image_paths = []
|
| 123 |
-
for file in os.listdir(tempfile.gettempdir()):
|
| 124 |
-
if file.endswith('.png'):
|
| 125 |
-
image_paths.append(os.path.join(tempfile.gettempdir(), file))
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
#
|
| 140 |
-
|
| 141 |
-
"react-system": """You are an helpful agent that helps children learn about Passover through coloring pages.
|
| 142 |
-
You have access to the following tools:
|
| 143 |
-
|
| 144 |
-
{tools_string}
|
| 145 |
-
|
| 146 |
-
When a user enters a number between 1 and 12, interpret it as the age of a child and generate Passover coloring pages suitable for that age using the generate_passover_coloring_pages tool.
|
| 147 |
-
If you're asked to generate coloring pages or if the input is just a number, assume the user wants Passover coloring pages for a child of that age.
|
| 148 |
-
|
| 149 |
-
To use a tool, output an action in this format:
|
| 150 |
-
```py
|
| 151 |
-
action = tool_name(arg1=value1, arg2=value2, ...)
|
| 152 |
-
```
|
| 153 |
-
|
| 154 |
-
Always use Python syntax for calling tools. After using a tool, analyze the result to determine what to do next. Use final_answer to return your answer when complete.
|
| 155 |
-
""",
|
| 156 |
-
"react-human": "{input_string}",
|
| 157 |
-
"react-assistant": "{agent_response}",
|
| 158 |
-
}
|
| 159 |
-
|
| 160 |
-
def main():
|
| 161 |
-
# Setup agent
|
| 162 |
-
final_answer = FinalAnswerTool()
|
| 163 |
-
|
| 164 |
-
model = HfApiModel(
|
| 165 |
-
max_tokens=2096,
|
| 166 |
-
temperature=0.5,
|
| 167 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
| 168 |
-
)
|
| 169 |
-
|
| 170 |
-
# Check if custom prompts should be used or merged with existing ones
|
| 171 |
-
try:
|
| 172 |
-
with open("prompts.yaml", 'r') as stream:
|
| 173 |
-
prompt_templates = yaml.safe_load(stream)
|
| 174 |
-
# Merge the templates, giving priority to our custom ones
|
| 175 |
-
prompt_templates.update(custom_prompt_templates)
|
| 176 |
-
except:
|
| 177 |
-
# If file doesn't exist or has issues, use our custom templates
|
| 178 |
-
prompt_templates = custom_prompt_templates
|
| 179 |
-
|
| 180 |
-
agent = CodeAgent(
|
| 181 |
-
model=model,
|
| 182 |
-
tools=[final_answer, generate_passover_coloring_pages],
|
| 183 |
-
max_steps=6,
|
| 184 |
-
verbosity_level=1,
|
| 185 |
-
prompt_templates=prompt_templates
|
| 186 |
-
)
|
| 187 |
-
|
| 188 |
-
# Create and launch Gradio interface
|
| 189 |
-
demo = create_gradio_interface()
|
| 190 |
-
demo.launch(share=False)
|
| 191 |
-
|
| 192 |
-
if __name__ == "__main__":
|
| 193 |
-
main()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
def echo(text):
|
| 4 |
+
return text
|
| 5 |
+
|
| 6 |
+
# Create a simple app
|
| 7 |
+
with gr.Blocks() as demo:
|
| 8 |
+
gr.Markdown("# API Test")
|
| 9 |
+
text_input = gr.Textbox(label="Input")
|
| 10 |
+
text_output = gr.Textbox(label="Output")
|
| 11 |
+
button = gr.Button("Echo")
|
| 12 |
+
button.click(echo, inputs=text_input, outputs=text_output)
|
| 13 |
+
|
| 14 |
+
# Try to directly access the API info
|
| 15 |
+
try:
|
| 16 |
+
api_info = demo.get_api_info()
|
| 17 |
+
print("API info successfully generated:")
|
| 18 |
+
print(f"Number of endpoints: {len(api_info['endpoints'])}")
|
| 19 |
+
print("First endpoint:", list(api_info['endpoints'].keys())[0])
|
| 20 |
+
print("Test successful! API info can be generated without errors.")
|
| 21 |
+
except Exception as e:
|
| 22 |
+
print(f"Error when trying to get API info: {type(e).__name__}: {str(e)}")
|
| 23 |
+
print("This is likely where your original error is coming from.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# Try to pinpoint the exact schema processing issue
|
|
|
|
|
|
|
|
|
|
| 26 |
try:
|
| 27 |
+
from gradio.blocks import Blocks
|
| 28 |
+
from gradio_client import utils as client_utils
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
print("\nTrying to manually process schema...")
|
| 31 |
+
blocks_instance = demo
|
| 32 |
+
if isinstance(blocks_instance, Blocks):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
try:
|
| 34 |
+
raw_api_info = blocks_instance.get_api_info()
|
| 35 |
+
print(" ✓ blocks_instance.get_api_info() succeeded")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
try:
|
| 38 |
+
# This is where many schema processing errors happen
|
| 39 |
+
processed_info = client_utils.json_schema_to_python_type(raw_api_info)
|
| 40 |
+
print(" ✓ json_schema_to_python_type succeeded")
|
| 41 |
+
except Exception as schema_err:
|
| 42 |
+
print(f" ✗ json_schema_to_python_type failed: {type(schema_err).__name__}: {str(schema_err)}")
|
| 43 |
+
print(" The error is in the schema processing part of Gradio")
|
| 44 |
+
except Exception as api_err:
|
| 45 |
+
print(f" ✗ blocks_instance.get_api_info() failed: {type(api_err).__name__}: {str(api_err)}")
|
| 46 |
+
except Exception as deep_err:
|
| 47 |
+
print(f"Error during deeper diagnosis: {type(deep_err).__name__}: {str(deep_err)}")
|
| 48 |
+
|
| 49 |
+
# Don't launch the app, we just want to test the API info generation
|
| 50 |
+
print("\nGradio version:", gr.__version__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|