File size: 1,770 Bytes
364513c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import gradio as gr
import requests
import os
import json

# The model you want to use — you can swap this for another if needed
MODEL_ID = "deepseek-ai/deepseek-coder-1.3b-instruct"

# Hugging Face token (set in your Space's "Variables and secrets")
HF_TOKEN = os.environ.get("HF_TOKEN")

API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}

def format_json(json_text):
    """
    Takes JSON text, sends it to the model, and returns formatted output.
    """
    try:
        # Validate JSON first
        parsed = json.loads(json_text)
    except json.JSONDecodeError:
        return "❌ That’s not valid JSON. Please check your brackets and quotes."

    # Create the prompt for the AI
    prompt = (
        "You are a document formatter. "
        "Take the following JSON data and turn it into a neat, human-readable layout "
        "that could be pasted into Google Docs with headings, bullet points, or tables:\n\n"
        f"{json_text}"
    )

    payload = {"inputs": prompt}

    # Send to Hugging Face Inference API
    response = requests.post(API_URL, headers=HEADERS, json=payload)
    try:
        data = response.json()
        if isinstance(data, list) and "generated_text" in data[0]:
            return data[0]["generated_text"]
        else:
            return str(data)
    except Exception as e:
        return f"Error parsing model response: {e}"

# Gradio interface
demo = gr.Interface(
    fn=format_json,
    inputs=gr.Textbox(lines=12, placeholder="Paste your JSON here..."),
    outputs="text",
    title="DeepSeek JSON → Docs Formatter",
    description="Paste JSON, get back neatly formatted text for Google Docs."
)

if __name__ == "__main__":
    demo.launch()