Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
import time
|
| 4 |
+
import os
|
| 5 |
+
from groq import Groq
|
| 6 |
+
from jsonschema import validate, ValidationError
|
| 7 |
+
|
| 8 |
+
MAX_RETRIES = 3
|
| 9 |
+
|
| 10 |
+
# Initialize Groq client
|
| 11 |
+
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def generate_structured_output(input_text, schema_text, model_name):
|
| 15 |
+
start_time = time.time()
|
| 16 |
+
retries = 0
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
schema = json.loads(schema_text)
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return None, f"❌ Invalid JSON Schema: {str(e)}", 0, 0
|
| 22 |
+
|
| 23 |
+
base_prompt = f"""
|
| 24 |
+
You are a strict JSON generator.
|
| 25 |
+
|
| 26 |
+
Follow this JSON schema exactly:
|
| 27 |
+
{schema_text}
|
| 28 |
+
|
| 29 |
+
Rules:
|
| 30 |
+
- Return ONLY valid JSON
|
| 31 |
+
- No explanation
|
| 32 |
+
- No markdown
|
| 33 |
+
- No extra text
|
| 34 |
+
|
| 35 |
+
Input:
|
| 36 |
+
{input_text}
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
+
while retries < MAX_RETRIES:
|
| 40 |
+
try:
|
| 41 |
+
response = client.chat.completions.create(
|
| 42 |
+
model=model_name,
|
| 43 |
+
messages=[{"role": "user", "content": base_prompt}],
|
| 44 |
+
temperature=0
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
output = response.choices[0].message.content.strip()
|
| 48 |
+
|
| 49 |
+
parsed = json.loads(output)
|
| 50 |
+
validate(instance=parsed, schema=schema)
|
| 51 |
+
|
| 52 |
+
latency = round(time.time() - start_time, 2)
|
| 53 |
+
|
| 54 |
+
return (
|
| 55 |
+
json.dumps(parsed, indent=2),
|
| 56 |
+
"✅ Valid JSON",
|
| 57 |
+
retries,
|
| 58 |
+
latency,
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
except (json.JSONDecodeError, ValidationError) as e:
|
| 62 |
+
retries += 1
|
| 63 |
+
error_message = str(e)
|
| 64 |
+
|
| 65 |
+
base_prompt = f"""
|
| 66 |
+
The previous output was invalid.
|
| 67 |
+
|
| 68 |
+
Error:
|
| 69 |
+
{error_message}
|
| 70 |
+
|
| 71 |
+
Fix the JSON to strictly match this schema:
|
| 72 |
+
{schema_text}
|
| 73 |
+
|
| 74 |
+
Return ONLY corrected JSON.
|
| 75 |
+
"""
|
| 76 |
+
|
| 77 |
+
latency = round(time.time() - start_time, 2)
|
| 78 |
+
|
| 79 |
+
return None, f"❌ Failed after {MAX_RETRIES} retries", retries, latency
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
with gr.Blocks(title="StructGuard-Groq") as demo:
|
| 83 |
+
gr.Markdown("# 🚀 StructGuard-Groq")
|
| 84 |
+
gr.Markdown("### Production-Grade Schema-First LLM Extractor (Powered by Groq)")
|
| 85 |
+
|
| 86 |
+
with gr.Row():
|
| 87 |
+
model_name = gr.Dropdown(
|
| 88 |
+
[
|
| 89 |
+
"llama3-70b-8192",
|
| 90 |
+
"mixtral-8x7b-32768",
|
| 91 |
+
"llama3-8b-8192"
|
| 92 |
+
],
|
| 93 |
+
value="llama3-70b-8192",
|
| 94 |
+
label="Groq Model"
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
input_text = gr.Textbox(
|
| 98 |
+
lines=8,
|
| 99 |
+
label="Input Text",
|
| 100 |
+
placeholder="Paste text you want to structure..."
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
schema_text = gr.Textbox(
|
| 104 |
+
lines=8,
|
| 105 |
+
label="JSON Schema",
|
| 106 |
+
placeholder='Paste valid JSON Schema here...'
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
generate_btn = gr.Button("Generate Structured JSON")
|
| 110 |
+
|
| 111 |
+
output_json = gr.Code(label="Validated JSON Output", language="json")
|
| 112 |
+
status = gr.Textbox(label="Validation Status")
|
| 113 |
+
retries_box = gr.Number(label="Retries Used")
|
| 114 |
+
latency_box = gr.Number(label="Latency (seconds)")
|
| 115 |
+
|
| 116 |
+
generate_btn.click(
|
| 117 |
+
generate_structured_output,
|
| 118 |
+
inputs=[input_text, schema_text, model_name],
|
| 119 |
+
outputs=[output_json, status, retries_box, latency_box],
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
demo.launch()
|