psychec / main.py
ejschwartz's picture
come on
446f43f
raw
history blame
3.43 kB
import gradio as gr
import subprocess
import tempfile
from pathlib import Path
EXAMPLE_CODE = """int main() {
x = 10;
y = foo(x);
return y;
}"""
def run_psychec(c_code: str):
"""Run PsycheC type inference on the provided C code."""
if not c_code.strip():
return "Please provide some C code.", "", ""
with tempfile.TemporaryDirectory() as tmpdir:
input_file = Path(tmpdir) / "input.c"
input_file.write_text(c_code)
try:
result = subprocess.run(
["python2", "/workspace/psychec/reconstruct.py", str(input_file)],
capture_output=True,
text=True,
timeout=30,
cwd="/workspace/psychec"
)
gen_header = Path(tmpdir) / "input_gen.h"
fixed_c = Path(tmpdir) / "input_fixed.c"
header_content = gen_header.read_text() if gen_header.exists() else "No header generated"
fixed_content = fixed_c.read_text() if fixed_c.exists() else "No fixed file generated"
log_output = result.stdout + ("\n" + result.stderr if result.stderr else "")
if not log_output.strip():
log_output = "Type inference completed successfully." if result.returncode == 0 else "Type inference failed."
return log_output, header_content, fixed_content
except subprocess.TimeoutExpired:
return "Error: Type inference timed out after 30 seconds.", "", ""
except Exception as e:
return f"Error: {e}", "", ""
def create_demo():
with gr.Blocks(title="PsycheC Type Inference") as demo:
gr.Markdown("# ๐Ÿ” PsycheC Type Inference")
gr.Markdown(
"Analyze C code and infer missing type declarations. "
"PsycheC will generate a header file with inferred types and a fixed C file."
)
with gr.Row():
with gr.Column():
code_input = gr.Code(
label="C Source Code",
language="c",
value=EXAMPLE_CODE,
lines=15
)
run_btn = gr.Button("Run Type Inference", variant="primary")
with gr.Column():
with gr.Tabs():
with gr.Tab("Log"):
log_output = gr.Textbox(label="Output Log", lines=10)
with gr.Tab("Generated Header"):
header_output = gr.Code(label="Generated Header (_gen.h)", language="c", lines=10)
with gr.Tab("Fixed C File"):
fixed_output = gr.Code(label="Fixed C File (_fixed.c)", language="c", lines=10)
run_btn.click(
fn=run_psychec,
inputs=[code_input],
outputs=[log_output, header_output, fixed_output]
)
gr.Markdown(
"---\n"
"Based on [PsycheC](https://github.com/ltcmelo/psychec) - "
"A compiler frontend for C with type inference capabilities.\n\n"
"Docker image: [psychec-typeinference-docker](https://github.com/edmcman/psychec-typeinference-docker)"
)
return demo
if __name__ == "__main__":
demo = create_demo()
demo.launch(server_name="0.0.0.0", server_port=7860)