TensorVizion commited on
Commit
203b146
·
verified ·
1 Parent(s): 0db4a3c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -1
app.py CHANGED
@@ -43,4 +43,57 @@ def extract_data(raw_text, fields_to_extract):
43
  # Fallback: Safely strip markdown code blocks without using complex regex
44
  # that might break code editors during copy-pasting
45
  if output_text.startswith("```"):
46
- # Remove the starting ```json or
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  # Fallback: Safely strip markdown code blocks without using complex regex
44
  # that might break code editors during copy-pasting
45
  if output_text.startswith("```"):
46
+ # Remove the starting ```json or
47
+ # Remove the ending ```
48
+ output_text = re.sub(r"\n?```$", "", output_text)
49
+
50
+ # Parse the text into an actual JSON dictionary for the Gradio UI
51
+ structured_data = json.loads(output_text.strip())
52
+ return structured_data
53
+
54
+ except json.JSONDecodeError:
55
+ return {
56
+ "error": "The model failed to return valid JSON. It returned this instead:",
57
+ "raw_output": output_text
58
+ }
59
+ except Exception as e:
60
+ return {"error": str(e)}
61
+
62
+ # -------------------------
63
+ # Build the Gradio UI
64
+ # -------------------------
65
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
66
+ gr.Markdown("# 🛟 The Data Rescuer")
67
+ gr.Markdown(f"**Powered by `{MODEL_ID}`** | Turn messy transcripts, notes, and OCR text into clean JSON data.")
68
+
69
+ with gr.Row():
70
+ # Left Column: Inputs
71
+ with gr.Column():
72
+ raw_input = gr.Textbox(
73
+ label="1. Paste Unstructured Text",
74
+ placeholder="Paste your messy meeting notes, emails, or raw text here...",
75
+ lines=12
76
+ )
77
+
78
+ schema_input = gr.Textbox(
79
+ label="2. What fields do you want to extract?",
80
+ placeholder="e.g., Company Name, Contact Person, Deadline, Action Items (list)",
81
+ lines=3
82
+ )
83
+
84
+ extract_btn = gr.Button("Extract Structured Data", variant="primary")
85
+
86
+ # Right Column: Output
87
+ with gr.Column():
88
+ json_output = gr.JSON(label="Structured Output")
89
+
90
+ # Connect the button to the function
91
+ extract_btn.click(
92
+ fn=extract_data,
93
+ inputs=[raw_input, schema_input],
94
+ outputs=json_output
95
+ )
96
+
97
+ # Launch the app
98
+ if __name__ == "__main__":
99
+ demo.launch()