File size: 9,559 Bytes
b34d81a
bbd191a
afa5179
b34d81a
 
 
 
 
 
 
d89074f
afa5179
b34d81a
afa5179
bbd191a
ae545ef
bbd191a
 
 
 
 
 
 
 
 
56d8eb0
bbd191a
 
56d8eb0
bbd191a
56d8eb0
bbd191a
 
b34d81a
 
 
 
 
 
 
 
 
 
 
56d8eb0
b34d81a
0787b7f
 
 
 
 
 
 
 
 
bbd191a
b34d81a
bbd191a
b34d81a
 
0787b7f
b34d81a
 
0787b7f
bbd191a
 
b34d81a
 
bbd191a
0787b7f
bbd191a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56d8eb0
bbd191a
 
 
 
 
56d8eb0
b34d81a
bbd191a
b34d81a
bbd191a
b34d81a
0787b7f
b34d81a
bbd191a
b34d81a
8f252c6
b34d81a
bbd191a
 
b34d81a
56d8eb0
b34d81a
bbd191a
b34d81a
bbd191a
 
b34d81a
bbd191a
8f252c6
ae545ef
bbd191a
b34d81a
bbd191a
0787b7f
 
 
bbd191a
0787b7f
bbd191a
0787b7f
56d8eb0
bbd191a
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import os
import json

import ollama
import gradio


OLLAMA_HOST = os.getenv('OLLAMA_HOST', "192.168.10.100:11434")


json_code = "```json\n{\n    \"classes\": [\n        \"http://data.europa.eu/949/ContactLineSystem\"\n    ],\n    \"properties\": [\n        \"http://data.europa.eu/949/contactLineSystemType\",\n        \"http://data.europa.eu/949/energySupplySystemTSICompliant\",\n        \"http://data.europa.eu/949/conditionsAppliedRegenerativeBraking\",\n        \"http://data.europa.eu/949/conditionalRegenerativeBrake\"\n    ]\n}\n```"

client_gpu_local = ollama.Client(host=f"http://{OLLAMA_HOST}", timeout=15)

model = "test_class_prop"

model_options = {
    #"top_k": 40,
    #"top_p": 0.95,
    "temperature": 0.5,
    "repeat_penalty": 1.1,
    #"num_ctx": 8*1024,
    #"num_ctx": 128*1024,
    "keep_alive": 60*60
  }

def clean_output():
    return None

def submit_query(msg):
    
    if msg != "":
        
        try:
    
            prompt = f"Identify the classes and properties used in this natural language query: \"{msg}\""
            
            messages = [{'role': 'user', 'content': prompt}]
            
            response = client_gpu_local.chat(
                model,
                messages=messages,
                options=model_options
            )

            msg = response.message.content
            
            debug = {
                k:v for k,v in response.dict().items()
            }
            
            debug["eval_t/s"] = debug["eval_count"]/(debug["eval_duration"]/1_000_000_000)
            debug["prompt_eval_t/s"] = debug["prompt_eval_count"]/(debug["prompt_eval_duration"]/1_000_000_000)
            
            print(response)

            try:

                json_data_test = json.loads(msg.split("```json")[1].split("```")[0])
                
                return [json.dumps(json_data_test, indent=2), json.dumps(debug, indent=2)]
            
            except:
                return [json.dumps({}, indent=2), json.dumps({"error": "Unable to process result."}, indent=2)]
            
        except:
            
            raise gradio.Error("Error: LLM request timed out!", duration=5)
    
    return [json.dumps({}, indent=2),json.dumps({"error": "No query"}, indent=2)]
        

test_dataset = [
        {
        "instruction": "Identify the classes and properties used in this natural language query: Fornisci il tipo di sistema di linea di contatto, la conformit\u00e0 TSI del sistema di fornitura energetica, l'umax2, le condizioni applicate al frenaggio regenerativo e la corrente massima del treno per i sistemi di linea di contatto. Limita a 1000 risultati.",
        "input": "",
        "output": "```json\n{\n    \"classes\": [\n        \"http://data.europa.eu/949/ContactLineSystem\"\n    ],\n    \"properties\": [\n        \"http://data.europa.eu/949/contactLineSystemType\",\n        \"http://data.europa.eu/949/energySupplySystemTSICompliant\",\n        \"http://data.europa.eu/949/umax2\",\n        \"http://data.europa.eu/949/conditionsAppliedRegenerativeBraking\",\n        \"http://data.europa.eu/949/maxTrainCurrent\"\n    ]\n}\n```"
    },
    {
        "instruction": "Identify the classes and properties used in this natural language query: Show me all unique combinations of the maximum magnetic field's directional components along the three axes (X, Y, Z) but only if the X component exceeds -63, and limit this to a total of 20 records.",
        "input": "",
        "output": "```json\n{\n    \"classes\": [\n        \"http://data.europa.eu/949/MaximumMagneticField\"\n    ],\n    \"properties\": [\n        \"http://data.europa.eu/949/maximumMagneticFieldDirectionZ\",\n        \"http://data.europa.eu/949/maximumMagneticFieldDirectionY\",\n        \"http://data.europa.eu/949/maximumMagneticFieldDirectionX\"\n    ]\n}\n```"
    },
    {
        "instruction": "Identify the classes and properties used in this natural language query: Je souhaite obtenir les coordonn\u00e9es intrins\u00e8ques de d\u00e9but (startIntrinsicCoordinate) et \u00e9ventuellement de fin (endIntrinsicCoordinate) pour chaque \u00e9l\u00e9ment r\u00e9seau (NetElement), en ne consid\u00e9rant que les 100 premiers r\u00e9sultats.",
        "input": "",
        "output": "```json\n{\n    \"classes\": [\n        \"http://data.europa.eu/949/NetElement\"\n    ],\n    \"properties\": [\n        \"http://data.europa.eu/949/endIntrinsicCoordinate\",\n        \"http://data.europa.eu/949/startIntrinsicCoordinate\"\n    ]\n}\n```"
    },
    {
        "instruction": "Identify the classes and properties used in this natural language query: Liefere mir bitte die Daten f\u00fcr assistanceStartingTrain (falls vorhanden), Curvature, Id, H\u00f6he und L\u00e4nge der PlatformEdge, wobei die L\u00e4nge nicht gleich -995.7379397044926 sein darf.",
        "input": "",
        "output": "```json\n{\n    \"classes\": [\n        \"http://data.europa.eu/949/PlatformEdge\"\n    ],\n    \"properties\": [\n        \"http://data.europa.eu/949/assistanceStartingTrain\",\n        \"http://data.europa.eu/949/hasPlatformCurvature\",\n        \"http://data.europa.eu/949/platformId\",\n        \"http://data.europa.eu/949/platformHeight\",\n        \"http://data.europa.eu/949/lengthOfPlatform\"\n    ]\n}\n```"
    },
    {
        "instruction": "Identify the classes and properties used in this natural language query: Mostra le informazioni distinte su SystemSeparationInfo, inclusi i valori per systemSeparationInfoLength, systemSeparationInfoChangeSupplySystem, systemSeparationInfoKm, systemSeparationInfoSwitchOffBreaker e systemSeparationInfoPantographLowered.",
        "input": "",
        "output": "```json\n{\n    \"classes\": [\n        \"http://data.europa.eu/949/SystemSeparationInfo\"\n    ],\n    \"properties\": [\n        \"http://data.europa.eu/949/systemSeparationInfoLength\",\n        \"http://data.europa.eu/949/systemSeparationInfoChangeSupplySystem\",\n        \"http://data.europa.eu/949/systemSeparationInfoKm\",\n        \"http://data.europa.eu/949/systemSeparationInfoSwitchOffBreaker\",\n        \"http://data.europa.eu/949/systemSeparationInfoPantographLowered\"\n    ]\n}\n```"
    },
    {
        "instruction": "Identify the classes and properties used in this natural language query: Elencare tutte le lunghezze univoche di tunnel che hanno una coordinata di partenza di -184.98148684158423.",
        "input": "",
        "output": "```json\n{\n    \"classes\": [\n        \"http://data.europa.eu/949/Tunnel\"\n    ],\n    \"properties\": [\n        \"http://data.europa.eu/949/lengthOfTunnel\",\n        \"http://data.europa.eu/949/startLocation\"\n    ]\n}\n```"
    },
    {
        "instruction": "Identify the classes and properties used in this natural language query: Mostra i valori di phaseInfoPantographLowered, phaseInfoLength e phaseInfoKm per tutte le istanze di PhaseInfo dove phaseInfoKm \u00e8 false.",
        "input": "",
        "output": "```json\n{\n    \"classes\": [\n        \"http://data.europa.eu/949/PhaseInfo\"\n    ],\n    \"properties\": [\n        \"http://data.europa.eu/949/phaseInfoPantographLowered\",\n        \"http://data.europa.eu/949/phaseInfoLength\",\n        \"http://data.europa.eu/949/phaseInfoKm\"\n    ]\n}\n```"
    },
    {
        "instruction": "Identify the classes and properties used in this natural language query: Donnez-moi les informations sur la nature, le d\u00e9but et la fin des op\u00e9rations ainsi que la longueur pour chaque section de ligne, en limitant les r\u00e9sultats \u00e0 100.",
        "input": "",
        "output": "```json\n{\n    \"classes\": [\n        \"http://data.europa.eu/949/SectionOfLine\"\n    ],\n    \"properties\": [\n        \"http://data.europa.eu/949/solNature\",\n        \"http://data.europa.eu/949/opStart\",\n        \"http://data.europa.eu/949/lengthOfSectionOfLine\",\n        \"http://data.europa.eu/949/opEnd\"\n    ]\n}\n```"
    },
]

examples_data = [[e["instruction"].split(": ")[1], e["output"].split("```json")[1].split("```")[0]] for e in test_dataset]


    
    

with gradio.Blocks() as demo:
    
    query = gradio.Textbox(render=False, label="Query", placeholder="Write a query and press Enter.", submit_btn="Send query")
   
    code_llm = gradio.Code(render=False, label="LLM output", interactive=False, language="json")
    metadata_llm = gradio.Code(render=False, label="LLM request metadata", interactive=False, language="json")
    code_ref = gradio.Code(render=False, label="Expected output", interactive=False, language="json")
        
    #chat = gradio.Chatbot(render=False, value=history, label="LLM output", type="messages")
    
    with gradio.Row():
        query.render()
            
    with gradio.Row():
        
        with gradio.Accordion(label="Examples", open=False):
            
            gradio.Examples(label="Query examples", examples=examples_data, example_labels=[e[0] for e in examples_data], cache_examples=False, inputs=[query, code_ref],examples_per_page=10)
    
            code_ref.render()
    #with gradio.Row():
        #chat.render()
        

        
    with gradio.Row():
        code_llm.render()
        
    with gradio.Row():
        metadata_llm.render()
         
    query.submit(submit_query,  inputs=[query], outputs=[code_llm, metadata_llm])
    #query.change(submit_query,  inputs=[query], outputs=[code_llm])
    query.change(clean_output, inputs=[], outputs=[code_llm]).then(submit_query,  inputs=[query], outputs=[code_llm, metadata_llm])

demo.launch()