sabssag commited on
Commit
1588aac
·
verified ·
1 Parent(s): 397f7c3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
+
5
+ # Load the fine-tuned model and tokenizer
6
+ model_path = '/content/drive/MyDrive/Model/model'
7
+ tokenizer_path = '/content/drive/MyDrive/Model/tokenizer'
8
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_path)
9
+ tokenizer = AutoTokenizer.from_pretrained(tokenizer_path)
10
+
11
+ # Function to generate Python code from LaTeX expression
12
+ def generate_code(latex_expr, tokenizer, model, max_length=512):
13
+ # Tokenize the input LaTeX expression
14
+ inputs = tokenizer.encode(
15
+ latex_expr,
16
+ return_tensors='pt',
17
+ max_length=max_length,
18
+ truncation=True
19
+ )
20
+
21
+ # Generate the output
22
+ with torch.no_grad():
23
+ outputs = model.generate(
24
+ inputs,
25
+ max_length=max_length,
26
+ num_beams=5, # Number of beams for beam search
27
+ early_stopping=True
28
+ )
29
+
30
+ # Decode the output tokens to string
31
+ generated_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
32
+
33
+ return generated_code
34
+
35
+ # Streamlit app layout
36
+ st.title("LaTeX to Python Code Generator")
37
+
38
+ # Define session state keys
39
+ if 'latex_expr' not in st.session_state:
40
+ st.session_state.latex_expr = ""
41
+
42
+ # User input for LaTeX expression
43
+ latex_input = st.text_area("Enter the LaTeX Expression", value=st.session_state.latex_expr, height=150)
44
+
45
+ # Update session state with the new LaTeX expression
46
+ if st.button("Generate Code"):
47
+ if latex_input:
48
+ st.session_state.latex_expr = latex_input
49
+ with st.spinner("Generating Python Code..."):
50
+ try:
51
+ generated_code = generate_code(latex_expr=st.session_state.latex_expr, tokenizer=tokenizer, model=model)
52
+ # Display the generated code
53
+ st.subheader("Generated Python Code")
54
+ st.code(generated_code, language='python')
55
+ except Exception as e:
56
+ st.error(f"Error during code generation: {e}")
57
+ else:
58
+ st.warning("Please enter a LaTeX expression to generate Python code.")