File size: 1,758 Bytes
b45d4d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
from pydantic import BaseModel, Field
from typing import List
from groq import Groq
import instructor
from dotenv import load_dotenv

import gradio as gr

load_dotenv()

client = Groq(
    api_key=os.getenv("GROQ_API_KEY"),
)
class Output(BaseModel):
    latex: str

client = instructor.from_groq(client, mode=instructor.Mode.TOOLS)


def get_LaTeX(query):
    prompt = f"""
    You are a tool which takes in natural language descriptions and give out LaTeX according to the instructions in the input
    Output LaTeX for the following:
    {query}
    """
    resp = client.chat.completions.create(
        model="llama3-8b-8192",
        messages=[
            {
                "role": "user",
                "content": prompt,
            }
        ],
        response_model=Output,
    )
    return resp.latex,"$$\\begin{align*}" + resp.latex + "\\end{align*}$$"

css = """
    #submit-btn {
        background: #8957E5;
    }
"""

with gr.Blocks(theme=gr.themes.Default(primary_hue="purple"),css=css) as demo:
    gr.HTML("<h1><center>Text to LaTeX<center><h1>")
    gr.HTML("<h3><center>Just type a query in natural language and get back LaTeX, you can verify it by looking at the rendered LaTeX!</center></h3>")
    with gr.Row():
        with gr.Column():
            input = gr.Textbox(label="Input Text")
            submit_btn = gr.Button(value='Submit',elem_id='submit-btn')
        with gr.Column():
            output = gr.Text(label='LaTeX',interactive=False)
            rendered_latex = gr.Markdown(label="Rendered LaTeX")
        
    gr.on(
        triggers=[input.submit, submit_btn.click],
        inputs=input,
        fn=get_LaTeX,
        outputs=[output,rendered_latex]
    )
if __name__ == "__main__":
    demo.launch()