Spaces:
Running
Running
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() | |