Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -399,106 +399,6 @@ def create_interface():
|
|
399 |
|
400 |
return demo
|
401 |
|
402 |
-
|
403 |
-
# Add FastAPI integration for smolagents
|
404 |
-
from fastapi import FastAPI, HTTPException
|
405 |
-
from pydantic import BaseModel
|
406 |
-
from typing import Optional
|
407 |
-
|
408 |
-
# Import smolagents
|
409 |
-
from smolagents.agent import Agent
|
410 |
-
|
411 |
-
# Create FastAPI app to mount with Gradio
|
412 |
-
api = FastAPI()
|
413 |
-
|
414 |
-
# Define API models
|
415 |
-
class ProductURLRequest(BaseModel):
|
416 |
-
url: str
|
417 |
-
format: Optional[str] = "json"
|
418 |
-
|
419 |
-
class ProductDataAgent(Agent):
|
420 |
-
def __init__(self):
|
421 |
-
super().__init__(
|
422 |
-
name="ProductDataAgent",
|
423 |
-
description="Extracts product data from IKEA URLs"
|
424 |
-
)
|
425 |
-
|
426 |
-
def run(self, url: str):
|
427 |
-
try:
|
428 |
-
extraction_result = extractor.extract_images_from_url(url)
|
429 |
-
return extraction_result.to_dict()
|
430 |
-
except Exception as e:
|
431 |
-
return {
|
432 |
-
"error": str(e),
|
433 |
-
"status": "failed"
|
434 |
-
}
|
435 |
-
|
436 |
-
@api.post("/api/extract")
|
437 |
-
async def extract_product_data(request: ProductURLRequest):
|
438 |
-
try:
|
439 |
-
agent = ProductDataAgent()
|
440 |
-
result = agent.run(request.url)
|
441 |
-
|
442 |
-
if request.format == "markdown":
|
443 |
-
# Add markdown formatting (implementation from smolagent_api.py)
|
444 |
-
def format_as_markdown(data: Dict[str, Any]) -> str:
|
445 |
-
"""Convert the extracted data to markdown format for nicer display"""
|
446 |
-
result = []
|
447 |
-
|
448 |
-
# Add request ID
|
449 |
-
result.append(f"### Request ID: {data.get('request_id', 'N/A')}")
|
450 |
-
result.append("")
|
451 |
-
|
452 |
-
# Add images section
|
453 |
-
result.append("### Images")
|
454 |
-
images = data.get("images", {})
|
455 |
-
if images:
|
456 |
-
for img_id, img_info in images.items():
|
457 |
-
result.append(f"- **ID**: {img_id}")
|
458 |
-
result.append(f" - **URL**: {img_info.get('url', 'N/A')}")
|
459 |
-
result.append(f" - **Type**: {img_info.get('type', 'unknown')}")
|
460 |
-
result.append(f" - **Alt**: {img_info.get('alt', 'N/A')}")
|
461 |
-
result.append("")
|
462 |
-
else:
|
463 |
-
result.append("No images found.")
|
464 |
-
result.append("")
|
465 |
-
|
466 |
-
# Add measurements section
|
467 |
-
result.append("### Measurements")
|
468 |
-
measurements = data.get("measurements", {})
|
469 |
-
if measurements:
|
470 |
-
for key, value in measurements.items():
|
471 |
-
result.append(f"- **{key.title()}**: {value}")
|
472 |
-
result.append("")
|
473 |
-
else:
|
474 |
-
result.append("No measurements found.")
|
475 |
-
result.append("")
|
476 |
-
|
477 |
-
# Add materials section
|
478 |
-
result.append("### Materials")
|
479 |
-
materials = data.get("materials", {})
|
480 |
-
if materials:
|
481 |
-
for key, value in materials.items():
|
482 |
-
result.append(f"- **{key.title()}**: {value}")
|
483 |
-
result.append("")
|
484 |
-
else:
|
485 |
-
result.append("No materials information found.")
|
486 |
-
result.append("")
|
487 |
-
|
488 |
-
return "\n".join(result)
|
489 |
-
result["markdown"] = format_as_markdown(result)
|
490 |
-
|
491 |
-
return {
|
492 |
-
"status": "success",
|
493 |
-
"data": result
|
494 |
-
}
|
495 |
-
except Exception as e:
|
496 |
-
raise HTTPException(status_code=500, detail=str(e))
|
497 |
-
|
498 |
-
# Update the launch code to include the FastAPI app
|
499 |
if __name__ == "__main__":
|
500 |
demo = create_interface()
|
501 |
-
demo.launch(
|
502 |
-
server_port=7860,
|
503 |
-
favicon_path=None,
|
504 |
-
app_kwargs={"app": api}) # Mount FastAPI on Gradio
|
|
|
399 |
|
400 |
return demo
|
401 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
402 |
if __name__ == "__main__":
|
403 |
demo = create_interface()
|
404 |
+
demo.launch()
|
|
|
|
|
|