File size: 2,471 Bytes
0232cb5
632dfc5
 
 
 
0232cb5
632dfc5
0232cb5
 
632dfc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0232cb5
632dfc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0232cb5
632dfc5
0232cb5
632dfc5
 
0232cb5
632dfc5
 
0232cb5
632dfc5
0232cb5
632dfc5
 
 
 
 
 
0232cb5
 
 
632dfc5
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
65
66
67
68
69
70
71
72
73
74
75
from huggingface_hub import InferenceClient
import ast
import nltk
import matplotlib.pyplot as plt
import gradio as gr

client = InferenceClient("Qwen/Qwen2.5-72B-Instruct")


def get_structures(sent):
    c_structure = ast.literal_eval(client.chat.completions.create(
        messages=[
            {"role": "system",
             "content": "generate bnf description for buiding c-structure according to lexical-functional grammar framework, no explanation or additional text, use the following structure:\n"
                        "c-structure: 'generated bnf description'"
             },
            {"role": "user",
             "content": f"generate bnf description for c-structure of the following sentence: {sent}"},
        ],
        response_format={
            "type": "json",
            "value": {
                "properties": {
                    "c-structure": {"type": "string"}},
            }
        },
        stream=False,
        max_tokens=512,
        temperature=0.7,
        top_p=0.1
    ).choices[0].get('message')['content'])

    c_latex = ast.literal_eval(client.chat.completions.create(
        messages=[
            {"role": "system",
             "content": "generate nltk respresentation for the LFG c-structure of the sentence according to provided bnf description, no explanation or additional text\n"
                        "example: (S (NP 'Text') (VP 'text')))"
             },
            {"role": "user",
             "content": f"description: {c_structure['c-structure']}"},
        ],
        response_format={
            "type": "json",
            "value": {
                "properties": {
                    "c-structure": {"type": "string"}},
            }
        },
        stream=False,
        max_tokens=512,
        temperature=0.7,
        top_p=0.1
    ).choices[0].get('message')['content'])

    tree = nltk.Tree.fromstring(c_latex['c-structure'])

    with open('output.txt', 'wt') as out:
        tree.pretty_print(stream=out)

    with open('output.txt', 'a') as f:
        f.write(f'c-structure:\n{c_latex["c-structure"]}\n\nBNF-description:\n{c_structure["c-structure"]}')

    return 'output.txt'

interface = gr.Interface(
    fn=get_structures,
    inputs=gr.Textbox(label="Enter your sentence"),
    outputs=gr.File(),
    title="LFG AI-Parser",
    description="Enter a sentence and visualize its c-structure according to LFG.",
)

if __name__ == "__main__":
    interface.launch(share=True)