Owadokun Tosin Tobi commited on
Commit
190c59b
·
unverified ·
1 Parent(s): f7fce54

Update main.py

Browse files

This 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.

Files changed (1) hide show
  1. app/main.py +38 -24
app/main.py CHANGED
@@ -1,42 +1,56 @@
1
  from fasthtml.common import *
2
- from app.components import Layout, InputForm, ResultDisplay
 
 
3
 
4
- app, rt = fast_app()
 
 
 
5
 
6
  @rt('/')
7
  def get():
8
- return Layout(
 
9
  "Structura | AI Data Architect",
10
- Div(
11
- InputForm(),
12
- Br(),
13
- Div(id="result-area") # The target for HTMX
14
- )
 
15
  )
16
 
17
  @rt('/extract')
18
  async def post(text_input: str, schema_type: str):
 
 
 
 
19
  if not text_input:
20
- return ResultDisplay(error="Please provide input text.")
21
-
22
- # --- AGENT CONNECTION ---
23
  try:
24
- # TODO: Connect to PydanticAI Agent here
25
- # real_data = await process_data(text_input, schema_type)
 
 
 
 
26
 
27
- # Mock Response to prove UI works
28
- import json
29
- mock_data = {
30
- "status": "success",
31
- "schema": schema_type,
32
- "extracted_data": "Agent logic coming in Batch 3"
33
- }
34
- json_str = json.dumps(mock_data, indent=2)
35
 
36
- return ResultDisplay(json_data=json_str)
 
37
 
38
  except Exception as e:
39
- return ResultDisplay(error=str(e))
 
40
 
41
- # Expose 'app' for the runner
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