Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
from huggingface_hub import hf_hub_download
|
5 |
+
from handwriting_api import InputData
|
6 |
+
from typing import List
|
7 |
+
|
8 |
+
# Download model from Hugging Face Hub
|
9 |
+
def download_model():
|
10 |
+
REPO_ID = os.getenv("HF_REPO_ID", "your_username/your_model")
|
11 |
+
FILENAMES = [
|
12 |
+
"checkpoints/model-17900.data-00000-of-00001",
|
13 |
+
"checkpoints/model-17900.index",
|
14 |
+
"checkpoints/model-17900.meta"
|
15 |
+
]
|
16 |
+
|
17 |
+
os.makedirs("checkpoints", exist_ok=True)
|
18 |
+
|
19 |
+
for filename in FILENAMES:
|
20 |
+
hf_hub_download(
|
21 |
+
repo_id=REPO_ID,
|
22 |
+
filename=filename,
|
23 |
+
local_dir=".",
|
24 |
+
token=os.getenv("HF_TOKEN")
|
25 |
+
)
|
26 |
+
|
27 |
+
# Download model on startup
|
28 |
+
try:
|
29 |
+
download_model()
|
30 |
+
except Exception as e:
|
31 |
+
raise RuntimeError(f"Model download failed: {str(e)}") from e
|
32 |
+
|
33 |
+
# Hugging Face Spaces requires app to run on port 7860
|
34 |
+
GRADIO_PORT = int(os.getenv("GRADIO_SERVER_PORT", "7860"))
|
35 |
+
def generate_handwriting_interface(text: str, style: int) -> str:
|
36 |
+
data = InputData(
|
37 |
+
text=text,
|
38 |
+
style=style,
|
39 |
+
bias=0.75,
|
40 |
+
stroke_colors=["#000000"],
|
41 |
+
stroke_widths=[2]
|
42 |
+
)
|
43 |
+
|
44 |
+
response = requests.post(
|
45 |
+
"http://localhost:8000/synthesize",
|
46 |
+
json=data.dict()
|
47 |
+
)
|
48 |
+
|
49 |
+
if response.status_code == 200:
|
50 |
+
return open("img/output.svg").read()
|
51 |
+
else:
|
52 |
+
return f"Error: {response.json()['detail']}"
|
53 |
+
|
54 |
+
with gr.Blocks() as demo:
|
55 |
+
gr.Markdown("# Handwriting Synthesis")
|
56 |
+
with gr.Row():
|
57 |
+
text_input = gr.Textbox(label="Input Text", lines=3, placeholder="Enter text to convert to handwriting...")
|
58 |
+
style_select = gr.Slider(minimum=0, maximum=12, step=1, label="Style", value=0)
|
59 |
+
|
60 |
+
output_svg = gr.HTML(label="Generated Handwriting")
|
61 |
+
download_button = gr.Button("Download SVG")
|
62 |
+
|
63 |
+
inputs = [text_input, style_select]
|
64 |
+
outputs = [output_svg]
|
65 |
+
|
66 |
+
generate_button = gr.Button("Generate")
|
67 |
+
generate_button.click(
|
68 |
+
fn=generate_handwriting_interface,
|
69 |
+
inputs=inputs,
|
70 |
+
outputs=outputs
|
71 |
+
)
|
72 |
+
|
73 |
+
download_button.click(
|
74 |
+
fn=lambda svg: svg,
|
75 |
+
inputs=output_svg,
|
76 |
+
outputs=gr.File(label="Download SVG")
|
77 |
+
)
|
78 |
+
|
79 |
+
demo.launch(
|
80 |
+
server_name="0.0.0.0",
|
81 |
+
server_port=GRADIO_PORT,
|
82 |
+
share=False,
|
83 |
+
show_error=True
|
84 |
+
)
|