Spaces:
Sleeping
Sleeping
app.py
CHANGED
@@ -1,73 +1,47 @@
|
|
1 |
import gradio as gr
|
2 |
-
import subprocess
|
3 |
-
import os
|
4 |
-
import uuid
|
5 |
-
import requests
|
6 |
-
|
7 |
-
# Optional: Set Typst binary path
|
8 |
-
TYPST_PATH = "typst"
|
9 |
-
|
10 |
-
# Temporary working directory
|
11 |
-
WORKDIR = "/tmp/sheikh_promptor"
|
12 |
-
|
13 |
-
os.makedirs(WORKDIR, exist_ok=True)
|
14 |
-
|
15 |
-
|
16 |
-
def build_typst_preview(code: str) -> tuple:
|
17 |
-
file_id = str(uuid.uuid4())
|
18 |
-
typ_path = os.path.join(WORKDIR, f"{file_id}.typ")
|
19 |
-
pdf_path = os.path.join(WORKDIR, f"{file_id}.pdf")
|
20 |
-
|
21 |
-
with open(typ_path, "w") as f:
|
22 |
-
f.write(code)
|
23 |
-
|
24 |
-
try:
|
25 |
-
subprocess.run([TYPST_PATH, "compile", typ_path, "-o", pdf_path],
|
26 |
-
check=True, capture_output=True)
|
27 |
-
return (pdf_path, None)
|
28 |
-
except subprocess.CalledProcessError as e:
|
29 |
-
return (None, e.stderr.decode())
|
30 |
-
|
31 |
-
|
32 |
-
def ai_generate_response(prompt: str, model="together") -> str:
|
33 |
-
# Add your Together/Groq API call here
|
34 |
-
if model == "together":
|
35 |
-
# Replace with real API integration
|
36 |
-
return f"[TogetherAI] ➜ Generated content for: {prompt}"
|
37 |
-
else:
|
38 |
-
return f"[GroqAI] ➜ Placeholder for: {prompt}"
|
39 |
-
|
40 |
-
|
41 |
-
def run_prompt_and_compile(prompt, model, code):
|
42 |
-
ai_result = ai_generate_response(prompt, model)
|
43 |
-
final_code = f"{code}\n\n// AI Suggestion:\n// {ai_result}"
|
44 |
-
pdf, err = build_typst_preview(final_code)
|
45 |
-
return final_code, ai_result, (pdf if pdf else None), (err if err else "Build succeeded.")
|
46 |
-
|
47 |
-
|
48 |
-
# Gradio UI
|
49 |
-
with gr.Blocks(css="body { font-family: Inter, sans-serif; }") as demo:
|
50 |
-
gr.Markdown("# **SheikhPromptor.run**")
|
51 |
-
|
52 |
-
with gr.Row():
|
53 |
-
code_editor = gr.Code(label="Typst/LaTeX/Markdown", language="typst", lines=20)
|
54 |
-
prompt_input = gr.Textbox(label="AI Prompt", placeholder="e.g. Generate a table in Typst")
|
55 |
-
model_select = gr.Dropdown(["together", "groq"], label="AI Model", value="together")
|
56 |
-
|
57 |
-
with gr.Row():
|
58 |
-
run_button = gr.Button("Run Prompt + Compile")
|
59 |
-
|
60 |
-
with gr.Row():
|
61 |
-
ai_output = gr.Textbox(label="AI Response")
|
62 |
-
|
63 |
-
with gr.Row():
|
64 |
-
pdf_preview = gr.File(label="PDF Output", file_types=[".pdf"])
|
65 |
-
compiler_log = gr.Textbox(label="Compiler Log")
|
66 |
-
|
67 |
-
run_button.click(
|
68 |
-
fn=run_prompt_and_compile,
|
69 |
-
inputs=[prompt_input, model_select, code_editor],
|
70 |
-
outputs=[code_editor, ai_output, pdf_preview, compiler_log]
|
71 |
-
)
|
72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
+
# HTML to inject Monaco Editor with Typst syntax (fallback to LaTeX for now)
|
4 |
+
monaco_html = """
|
5 |
+
<div id="monaco-container" style="width:100%; height: 500px;"></div>
|
6 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.33.0/min/vs/loader.js"></script>
|
7 |
+
<script>
|
8 |
+
require.config({ paths: { vs: 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.33.0/min/vs' }});
|
9 |
+
require(['vs/editor/editor.main'], function() {
|
10 |
+
var editor = monaco.editor.create(document.getElementById('monaco-container'), {
|
11 |
+
value: '',
|
12 |
+
language: 'latex', # Use 'latex' here, you can configure a custom Typst language mode if available
|
13 |
+
theme: 'vs-dark',
|
14 |
+
automaticLayout: true
|
15 |
+
});
|
16 |
+
editor.onDidChangeModelContent(function() {
|
17 |
+
var value = editor.getValue();
|
18 |
+
document.getElementById("monaco-input").value = value; # Push content to hidden textbox
|
19 |
+
});
|
20 |
+
});
|
21 |
+
</script>
|
22 |
+
"""
|
23 |
+
|
24 |
+
# Gradio Blocks setup
|
25 |
+
with gr.Blocks() as demo:
|
26 |
+
# Hidden Textbox to store content from Monaco editor
|
27 |
+
code_input = gr.Textbox(visible=False, label="Typst Source")
|
28 |
+
|
29 |
+
# Inject HTML for Monaco Editor
|
30 |
+
gr.HTML(monaco_html)
|
31 |
+
|
32 |
+
# Optional: Trigger AI, prompt generation, etc., based on the input
|
33 |
+
output = gr.Textbox(label="Output") # For AI responses or LaTeX rendering
|
34 |
+
|
35 |
+
# Function to process Typst input and generate response
|
36 |
+
def process_input(typst_input):
|
37 |
+
# Placeholder for processing input (could be LaTeX rendering or AI response)
|
38 |
+
return f"Processed Typst Input:\n{typst_input}"
|
39 |
+
|
40 |
+
# When Monaco Editor content changes, process the input
|
41 |
+
code_input.change(process_input, inputs=code_input, outputs=output)
|
42 |
+
|
43 |
+
# Optional: Additional output or behavior
|
44 |
+
gr.HTML("<h3>Generate Typst or LaTeX output with AI or live preview</h3>")
|
45 |
+
|
46 |
+
# Launch the Gradio interface
|
47 |
demo.launch()
|