Owadokun Tosin Tobi commited on
Update main.py
Browse filesThis file handles what it does. It imports the components and connects them to the AI backend. It is now clean, readable, and focused on routing.
- app/main.py +38 -24
app/main.py
CHANGED
|
@@ -1,42 +1,56 @@
|
|
| 1 |
from fasthtml.common import *
|
| 2 |
-
from
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
@rt('/')
|
| 7 |
def get():
|
| 8 |
-
|
|
|
|
| 9 |
"Structura | AI Data Architect",
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
| 15 |
)
|
| 16 |
|
| 17 |
@rt('/extract')
|
| 18 |
async def post(text_input: str, schema_type: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
if not text_input:
|
| 20 |
-
return
|
| 21 |
-
|
| 22 |
-
# --- AGENT CONNECTION ---
|
| 23 |
try:
|
| 24 |
-
#
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
}
|
| 34 |
-
json_str = json.dumps(mock_data, indent=2)
|
| 35 |
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
except Exception as e:
|
| 39 |
-
|
|
|
|
| 40 |
|
| 41 |
-
# Expose
|
| 42 |
target = app
|
|
|
|
| 1 |
from fasthtml.common import *
|
| 2 |
+
from agents.extractor import process_data
|
| 3 |
+
from app.components import PageLayout, HeroSection, ExtractionForm, SuccessDisplay, ErrorDisplay, LoadingIndicator
|
| 4 |
+
import json
|
| 5 |
|
| 6 |
+
# Initialize App
|
| 7 |
+
app, rt = fast_app(
|
| 8 |
+
hdrs=(picolink,)
|
| 9 |
+
)
|
| 10 |
|
| 11 |
@rt('/')
|
| 12 |
def get():
|
| 13 |
+
"""Renders the Home Page"""
|
| 14 |
+
return PageLayout(
|
| 15 |
"Structura | AI Data Architect",
|
| 16 |
+
[
|
| 17 |
+
HeroSection(),
|
| 18 |
+
ExtractionForm(),
|
| 19 |
+
LoadingIndicator(),
|
| 20 |
+
Div(id="result-area") # Target for HTMX results
|
| 21 |
+
]
|
| 22 |
)
|
| 23 |
|
| 24 |
@rt('/extract')
|
| 25 |
async def post(text_input: str, schema_type: str):
|
| 26 |
+
"""
|
| 27 |
+
Handles the HTMX POST request.
|
| 28 |
+
Returns ONLY the result component (HTML snippet), not the full page.
|
| 29 |
+
"""
|
| 30 |
if not text_input:
|
| 31 |
+
return ErrorDisplay("Please provide input text to analyze.")
|
| 32 |
+
|
|
|
|
| 33 |
try:
|
| 34 |
+
# 1. Call the PydanticAI Agent
|
| 35 |
+
data = await process_data(text_input, schema_type)
|
| 36 |
+
|
| 37 |
+
# 2. Check for Agent-level errors
|
| 38 |
+
if isinstance(data, dict) and "error" in data:
|
| 39 |
+
return ErrorDisplay(f"Agent Error: {data['error']}")
|
| 40 |
|
| 41 |
+
# 3. Serialize Pydantic Model to JSON
|
| 42 |
+
# Robust check: if it's already a dict/list, dump it; if it's a Pydantic model, use model_dump_json
|
| 43 |
+
if hasattr(data, 'model_dump_json'):
|
| 44 |
+
json_str = data.model_dump_json(indent=2)
|
| 45 |
+
else:
|
| 46 |
+
json_str = json.dumps(data, indent=2)
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
# 4. Return Success Component
|
| 49 |
+
return SuccessDisplay(json_str)
|
| 50 |
|
| 51 |
except Exception as e:
|
| 52 |
+
# 5. Handle System Crashes
|
| 53 |
+
return ErrorDisplay(f"System Critical: {str(e)}")
|
| 54 |
|
| 55 |
+
# Expose app for Uvicorn
|
| 56 |
target = app
|