sabssag commited on
Commit
0652586
·
verified ·
1 Parent(s): 35ad71b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -20
app.py CHANGED
@@ -8,27 +8,15 @@ model = AutoModelForSeq2SeqLM.from_pretrained(model_repo_path)
8
  tokenizer = AutoTokenizer.from_pretrained(model_repo_path)
9
 
10
  # Function to generate Python code from LaTeX expression
11
- def generate_code(latex_expr, tokenizer, model, max_length=512):
12
- # Tokenize the input LaTeX expression
13
- inputs = tokenizer.encode(
14
- latex_expr,
15
- return_tensors='pt',
16
- max_length=max_length,
17
- truncation=True
18
- )
19
-
20
  # Generate the output
21
- with torch.no_grad():
22
- outputs = model.generate(
23
- inputs,
24
- max_length=max_length,
25
- num_beams=5, # Number of beams for beam search
26
- early_stopping=True
27
- )
28
-
29
- # Decode the output tokens to string
30
  generated_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
31
-
32
  return generated_code
33
 
34
  # Streamlit app layout
@@ -47,7 +35,7 @@ if st.button("Generate Code"):
47
  st.session_state.latex_expr = latex_input
48
  with st.spinner("Generating Python Code..."):
49
  try:
50
- generated_code = generate_code(latex_expr=st.session_state.latex_expr, tokenizer=tokenizer, model=model)
51
  # Display the generated code
52
  st.subheader("Generated Python Code")
53
  st.code(generated_code, language='python')
 
8
  tokenizer = AutoTokenizer.from_pretrained(model_repo_path)
9
 
10
  # Function to generate Python code from LaTeX expression
11
+ def generate_code_from_latex(latex_expression, max_length=256):
12
+ inputs = tokenizer(f"Latex Expression: {latex_expression} Solution:", return_tensors="pt").to(model.device)
13
+
 
 
 
 
 
 
14
  # Generate the output
15
+ outputs = model.generate(**inputs, max_length=max_length)
16
+
17
+ # Decode the output into Python code
 
 
 
 
 
 
18
  generated_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
19
+
20
  return generated_code
21
 
22
  # Streamlit app layout
 
35
  st.session_state.latex_expr = latex_input
36
  with st.spinner("Generating Python Code..."):
37
  try:
38
+ generated_code = generate_code(latex_expr=st.session_state.latex_expr)
39
  # Display the generated code
40
  st.subheader("Generated Python Code")
41
  st.code(generated_code, language='python')