sabssag commited on
Commit
c7260b4
1 Parent(s): de4950e

Create app.py

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