Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
import json
|
5 |
+
|
6 |
+
# The model you want to use — you can swap this for another if needed
|
7 |
+
MODEL_ID = "deepseek-ai/deepseek-coder-1.3b-instruct"
|
8 |
+
|
9 |
+
# Hugging Face token (set in your Space's "Variables and secrets")
|
10 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
11 |
+
|
12 |
+
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
|
13 |
+
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
|
14 |
+
|
15 |
+
def format_json(json_text):
|
16 |
+
"""
|
17 |
+
Takes JSON text, sends it to the model, and returns formatted output.
|
18 |
+
"""
|
19 |
+
try:
|
20 |
+
# Validate JSON first
|
21 |
+
parsed = json.loads(json_text)
|
22 |
+
except json.JSONDecodeError:
|
23 |
+
return "❌ That’s not valid JSON. Please check your brackets and quotes."
|
24 |
+
|
25 |
+
# Create the prompt for the AI
|
26 |
+
prompt = (
|
27 |
+
"You are a document formatter. "
|
28 |
+
"Take the following JSON data and turn it into a neat, human-readable layout "
|
29 |
+
"that could be pasted into Google Docs with headings, bullet points, or tables:\n\n"
|
30 |
+
f"{json_text}"
|
31 |
+
)
|
32 |
+
|
33 |
+
payload = {"inputs": prompt}
|
34 |
+
|
35 |
+
# Send to Hugging Face Inference API
|
36 |
+
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
37 |
+
try:
|
38 |
+
data = response.json()
|
39 |
+
if isinstance(data, list) and "generated_text" in data[0]:
|
40 |
+
return data[0]["generated_text"]
|
41 |
+
else:
|
42 |
+
return str(data)
|
43 |
+
except Exception as e:
|
44 |
+
return f"Error parsing model response: {e}"
|
45 |
+
|
46 |
+
# Gradio interface
|
47 |
+
demo = gr.Interface(
|
48 |
+
fn=format_json,
|
49 |
+
inputs=gr.Textbox(lines=12, placeholder="Paste your JSON here..."),
|
50 |
+
outputs="text",
|
51 |
+
title="DeepSeek JSON → Docs Formatter",
|
52 |
+
description="Paste JSON, get back neatly formatted text for Google Docs."
|
53 |
+
)
|
54 |
+
|
55 |
+
if __name__ == "__main__":
|
56 |
+
demo.launch()
|