vermen commited on
Commit
52cdd23
verified
1 Parent(s): 4189f91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -0
app.py CHANGED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import subprocess
2
+ import os
3
+ from PIL import Image
4
+ ##################################
5
+ def write_txt(file_name,text):
6
+ with open(file_name, "w", encoding="utf-8") as file:
7
+ file.write(text)
8
+ ##################################
9
+ def load_txt(file_name):
10
+ with open(file_name, "r", encoding="utf-8") as file:
11
+ text = file.read()
12
+ return text
13
+ ####
14
+ os.system("apt-get install -y --no-install-recommends texlive-latex-base texlive-latex-recommended texlive-pictures texlive-science texlive-latex-extra poppler-utils")
15
+ print("all latex installed")
16
+ ####
17
+
18
+ def extract_document_content(latex_code):
19
+ """
20
+ Extract the content between \begin{document} and \end{document} in LaTeX code.
21
+
22
+ Args:
23
+ latex_code (str): The LaTeX code to extract from
24
+
25
+ Returns:
26
+ str: The extracted content, or None if no document environment is found
27
+ """
28
+ start_tag = "\\begin{document}"
29
+ end_tag = "\\end{document}"
30
+
31
+ start_index = latex_code.find(start_tag)
32
+ end_index = latex_code.find(end_tag)
33
+
34
+ if start_index == -1 or end_index == -1:
35
+ return None
36
+
37
+ # Add the length of the start tag to get the content right after it
38
+ start_content_index = start_index + len(start_tag)
39
+
40
+ # Extract the content between the tags
41
+ document_content = latex_code[start_content_index:end_index]
42
+
43
+ return document_content.strip()
44
+ ##################################
45
+ from google import genai
46
+
47
+ GEMINI_API_KEY = "AIzaSyB8pCfu9a3Bx7L6v1JyTdMMNf4W55B5D08"
48
+
49
+ client = genai.Client(api_key=GEMINI_API_KEY)
50
+
51
+ from google.genai import types
52
+ def generate_tex(file_path):
53
+ print(file_path)
54
+ myfile = client.files.upload(file=file_path)
55
+
56
+ result = client.models.generate_content(
57
+ model="gemini-2.0-flash",#"gemini-1.5-pro",#
58
+ contents=[
59
+ myfile,
60
+ "\n\n",
61
+ "Act as an expert in tikzcd. Make the tikcd code of the image, include packages for special characters.\n"+
62
+ "The document class is always standalone, and include: \\usetikzlibrary{decorations.pathmorphing}, \\usepackage{stmaryrd}, \\usepackage{amssymb} after the tikcd library."+
63
+ "Do not make any suggestion nor commentaries."
64
+ ],
65
+ config=types.GenerateContentConfig(temperature=0.1)
66
+ )
67
+
68
+
69
+ #tex_code = load_txt("latex_code.tex")
70
+ return result.text
71
+
72
+ def generate_pdf_png(latex_code):
73
+ ## output latex_code.pdf
74
+ # remove
75
+ latex_code = latex_code.replace("```tex","")
76
+ latex_code = latex_code.replace("```latex","")
77
+ latex_code = latex_code.replace("```","")
78
+ write_txt("latex_code.tex",latex_code)
79
+ print("file saved")
80
+ # Compile with pdflatex
81
+ #os.system("pdflatex latex_code.tex")
82
+ import subprocess
83
+ try:
84
+ # Set a timeout (in seconds)
85
+ result = subprocess.run(['pdflatex'],input=b'latex_code.tex\n', timeout=20,
86
+ stdout=subprocess.PIPE,
87
+ stderr=subprocess.PIPE)
88
+ print(result.stdout.decode())
89
+ except subprocess.TimeoutExpired:
90
+ print("Command timed out")
91
+ # Convert PDF to image using pdf2image (needs poppler installed)
92
+ pdf_file = "latex_code.pdf"
93
+ print("already have a pdf")
94
+ # save as pdf
95
+ from pdf2image import convert_from_path
96
+ images = convert_from_path(pdf_file)
97
+ image = images[0]
98
+ image.save("latex_code.png")
99
+ print("Image saved as latex_code.png")
100
+
101
+ def generate_results(file_path):
102
+ tikcd_code = generate_tex(file_path) # filepath of the image, as input for the generative model
103
+ generate_pdf_png(tikcd_code)
104
+ return ("./latex_code.png",extract_document_content(tikcd_code))
105
+ #
106
+ print("all was initialized\n")
107
+ #write_txt("aux.tex","Hello world")
108
+ #
109
+ import gradio as gr
110
+ app = gr.Interface(
111
+ fn= generate_results,
112
+ inputs=gr.Image(type="filepath"),
113
+ outputs=[gr.Image(label="Imagen latex"),gr.TextArea(label="C贸digo latex",lines=30)]
114
+ )
115
+
116
+ ####
117
+ #port = int(os.environ.get("PORT", 8080))
118
+ app.launch(share=True)#server_name="0.0.0.0", server_port=port,debug=True)