Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,16 @@
|
|
| 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 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
| 11 |
def generate_handwriting_interface(text: str, style: int) -> str:
|
| 12 |
data = InputData(
|
| 13 |
text=text,
|
|
@@ -17,44 +20,44 @@ def generate_handwriting_interface(text: str, style: int) -> str:
|
|
| 17 |
stroke_widths=[2]
|
| 18 |
)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
)
|
| 24 |
-
|
| 25 |
-
if response.status_code == 200:
|
| 26 |
-
return open("img/output.svg").read()
|
| 27 |
-
else:
|
| 28 |
-
return f"Error: {response.json()['detail']}"
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
with gr.Blocks() as demo:
|
| 31 |
gr.Markdown("# Handwriting Synthesis")
|
| 32 |
with gr.Row():
|
| 33 |
-
text_input = gr.Textbox(label="Input Text", lines=3
|
| 34 |
style_select = gr.Slider(minimum=0, maximum=12, step=1, label="Style", value=0)
|
| 35 |
|
| 36 |
output_svg = gr.HTML(label="Generated Handwriting")
|
| 37 |
-
download_button = gr.Button("Download SVG")
|
| 38 |
-
|
| 39 |
-
inputs = [text_input, style_select]
|
| 40 |
-
outputs = [output_svg]
|
| 41 |
-
|
| 42 |
generate_button = gr.Button("Generate")
|
|
|
|
| 43 |
generate_button.click(
|
| 44 |
fn=generate_handwriting_interface,
|
| 45 |
-
inputs=
|
| 46 |
-
outputs=
|
| 47 |
-
)
|
| 48 |
-
|
| 49 |
-
download_button.click(
|
| 50 |
-
fn=lambda svg: svg,
|
| 51 |
-
inputs=output_svg,
|
| 52 |
-
outputs=gr.File(label="Download SVG")
|
| 53 |
)
|
| 54 |
|
| 55 |
-
demo.launch(
|
| 56 |
-
server_name="0.0.0.0",
|
| 57 |
-
server_port=GRADIO_PORT,
|
| 58 |
-
share=False,
|
| 59 |
-
show_error=True
|
| 60 |
-
)
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import os
|
| 3 |
from huggingface_hub import hf_hub_download
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# 1. Import your model logic directly, not via HTTP
|
| 6 |
+
from handwriting_api import InputData, validate_input
|
| 7 |
+
from hand import Hand
|
| 8 |
|
| 9 |
+
|
| 10 |
+
# 2. Initialize your model once
|
| 11 |
+
hand = Hand()
|
| 12 |
+
|
| 13 |
+
# 3. Directly call the model logic in the Gradio function
|
| 14 |
def generate_handwriting_interface(text: str, style: int) -> str:
|
| 15 |
data = InputData(
|
| 16 |
text=text,
|
|
|
|
| 20 |
stroke_widths=[2]
|
| 21 |
)
|
| 22 |
|
| 23 |
+
# Validate the input
|
| 24 |
+
try:
|
| 25 |
+
validate_input(data)
|
| 26 |
+
except ValueError as e:
|
| 27 |
+
return f"Error: {str(e)}"
|
| 28 |
+
|
| 29 |
+
# Generate the handwriting
|
| 30 |
+
lines = data.text.split('\n')
|
| 31 |
+
biases = [data.bias] * len(lines)
|
| 32 |
+
styles = [data.style] * len(lines)
|
| 33 |
+
|
| 34 |
+
hand.write(
|
| 35 |
+
filename='img/output.svg',
|
| 36 |
+
lines=lines,
|
| 37 |
+
biases=biases,
|
| 38 |
+
styles=styles,
|
| 39 |
+
stroke_colors=data.stroke_colors,
|
| 40 |
+
stroke_widths=data.stroke_widths
|
| 41 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
# Return the SVG contents
|
| 44 |
+
with open("img/output.svg", "r") as f:
|
| 45 |
+
return f.read()
|
| 46 |
+
|
| 47 |
+
# 4. Set up the Gradio UI
|
| 48 |
with gr.Blocks() as demo:
|
| 49 |
gr.Markdown("# Handwriting Synthesis")
|
| 50 |
with gr.Row():
|
| 51 |
+
text_input = gr.Textbox(label="Input Text", lines=3)
|
| 52 |
style_select = gr.Slider(minimum=0, maximum=12, step=1, label="Style", value=0)
|
| 53 |
|
| 54 |
output_svg = gr.HTML(label="Generated Handwriting")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
generate_button = gr.Button("Generate")
|
| 56 |
+
|
| 57 |
generate_button.click(
|
| 58 |
fn=generate_handwriting_interface,
|
| 59 |
+
inputs=[text_input, style_select],
|
| 60 |
+
outputs=output_svg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
)
|
| 62 |
|
| 63 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|