Spaces:
Runtime error
Runtime error
File size: 1,843 Bytes
1588aac 94b7208 1588aac c5b2a7c d74b748 b1f44d7 1588aac 94b7208 0652586 1588aac 0652586 1588aac 0652586 1588aac 94b7208 1588aac 94b7208 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import streamlit as st
import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
# Load the fine-tuned model and tokenizer
model_repo_path = 'sabssag/Latex_to_Python_T5-small'
model = AutoModelForSeq2SeqLM.from_pretrained(model_repo_path)
tokenizer = AutoTokenizer.from_pretrained(model_repo_path)
model.eval()
# Function to generate Python code from LaTeX expression
def generate_code_from_latex(latex_expression, max_length=256):
inputs = tokenizer(f"Latex Expression: {latex_expression} Solution:", return_tensors="pt").to(model.device)
# Generate the output
outputs = model.generate(**inputs, max_length=max_length)
# Decode the output into Python code
generated_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
return generated_code
# Streamlit app layout
st.title("LaTeX to Python Code Generator")
# Define session state keys
if 'latex_expr' not in st.session_state:
st.session_state.latex_expr = ""
# User input for LaTeX expression
latex_input = st.text_area("Enter the LaTeX Expression", value=st.session_state.latex_expr, height=150)
# Update session state with the new LaTeX expression
if st.button("Generate Code"):
if latex_input:
st.session_state.latex_expr = latex_input
with st.spinner("Generating Python Code..."):
try:
# Correct function name here
generated_code = generate_code_from_latex(latex_expression=st.session_state.latex_expr)
# Display the generated code
st.subheader("Generated Python Code")
st.code(generated_code, language='python')
except Exception as e:
st.error(f"Error during code generation: {e}")
else:
st.warning("Please enter a LaTeX expression to generate Python code.")
|