Spaces:
Running
Running
Commit
·
bbd191a
1
Parent(s):
56d8eb0
Test
Browse files
app.py
CHANGED
@@ -1,53 +1,133 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
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```"
|
4 |
|
|
|
5 |
|
6 |
-
|
7 |
-
if "test" in message.lower():
|
8 |
-
return "Type Python or JavaScript to see the code.", gr.Code(language="json", value=json_code)
|
9 |
-
else:
|
10 |
-
return "Please ask about Python or JavaScript.", None
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
#{"role": "assistant", "content": gr.Plot(value=make_plot_from_file('quaterly_sales.txt'))}
|
16 |
-
]
|
17 |
|
18 |
-
|
19 |
|
20 |
-
|
21 |
|
22 |
-
|
23 |
-
gr.Chatbot(history, label="LLM output", type="messages")
|
24 |
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
'''
|
32 |
with gr.Blocks() as demo:
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
|
|
|
|
|
|
36 |
with gr.Row():
|
37 |
|
38 |
-
gr.
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
)
|
45 |
|
46 |
-
with gr.Row():
|
47 |
-
code.render()
|
48 |
|
|
|
49 |
with gr.Row():
|
50 |
-
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
|
53 |
-
'''
|
|
|
1 |
import gradio as gr
|
2 |
+
import ollama
|
3 |
+
import json
|
4 |
|
5 |
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```"
|
6 |
|
7 |
+
client_gpu_local = ollama.Client(host='http://192.168.10.100:11434')
|
8 |
|
9 |
+
model = "test_class_prop"
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
model_options = {
|
12 |
+
#"top_k": 40,
|
13 |
+
#"top_p": 0.95,
|
14 |
+
"temperature": 0.5,
|
15 |
+
"repeat_penalty": 1.1,
|
16 |
+
#"num_ctx": 8*1024,
|
17 |
+
#"num_ctx": 128*1024,
|
18 |
+
"keep_alive": 60*60
|
19 |
+
}
|
20 |
|
21 |
+
def clean_output():
|
22 |
+
return None
|
|
|
|
|
23 |
|
24 |
+
def submit_query(msg):
|
25 |
|
26 |
+
if msg != "":
|
27 |
|
28 |
+
prompt = f"Identify the classes and properties used in this natural language query: \"{msg}\""
|
|
|
29 |
|
30 |
+
messages = [{'role': 'user', 'content': prompt}]
|
31 |
+
|
32 |
+
response = client_gpu_local.chat(
|
33 |
+
model,
|
34 |
+
messages=messages,
|
35 |
+
options=model_options
|
36 |
+
)
|
37 |
|
38 |
+
msg = response.message.content
|
39 |
+
|
40 |
+
try:
|
41 |
+
|
42 |
+
json_data_test = json.loads(msg.split("```json")[1].split("```")[0])
|
43 |
+
|
44 |
+
return json.dumps(json_data_test, indent=2)
|
45 |
+
|
46 |
+
except:
|
47 |
+
return "{}"
|
48 |
+
|
49 |
+
return "{}"
|
50 |
+
|
51 |
+
|
52 |
+
test_dataset = [
|
53 |
+
{
|
54 |
+
"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.",
|
55 |
+
"input": "",
|
56 |
+
"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```"
|
57 |
+
},
|
58 |
+
{
|
59 |
+
"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.",
|
60 |
+
"input": "",
|
61 |
+
"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```"
|
62 |
+
},
|
63 |
+
{
|
64 |
+
"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.",
|
65 |
+
"input": "",
|
66 |
+
"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```"
|
67 |
+
},
|
68 |
+
{
|
69 |
+
"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.",
|
70 |
+
"input": "",
|
71 |
+
"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```"
|
72 |
+
},
|
73 |
+
{
|
74 |
+
"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.",
|
75 |
+
"input": "",
|
76 |
+
"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```"
|
77 |
+
},
|
78 |
+
{
|
79 |
+
"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.",
|
80 |
+
"input": "",
|
81 |
+
"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```"
|
82 |
+
},
|
83 |
+
{
|
84 |
+
"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.",
|
85 |
+
"input": "",
|
86 |
+
"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```"
|
87 |
+
},
|
88 |
+
{
|
89 |
+
"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.",
|
90 |
+
"input": "",
|
91 |
+
"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```"
|
92 |
+
},
|
93 |
+
]
|
94 |
|
95 |
+
examples_data = [[e["instruction"].split(": ")[1], e["output"].split("```json")[1].split("```")[0]] for e in test_dataset]
|
96 |
+
|
97 |
+
|
98 |
+
|
99 |
+
|
100 |
|
|
|
101 |
with gr.Blocks() as demo:
|
102 |
+
|
103 |
+
query = gr.Textbox(render=False, label="Query", placeholder="Write a query and press Enter.", submit_btn="Send query")
|
104 |
+
|
105 |
+
|
106 |
+
code_llm = gr.Code(render=False, label="LLM output", interactive=False, language="json")
|
107 |
+
code_ref = gr.Code(render=False, label="Expected output", interactive=False, language="json")
|
108 |
+
|
109 |
+
#chat = gr.Chatbot(render=False, value=history, label="LLM output", type="messages")
|
110 |
|
111 |
+
with gr.Row():
|
112 |
+
query.render()
|
113 |
+
|
114 |
with gr.Row():
|
115 |
|
116 |
+
with gr.Accordion(label="Examples", open=False):
|
117 |
+
|
118 |
+
gr.Examples(label="Query examples", examples=examples_data, example_labels=[e[0] for e in examples_data], cache_examples=False, fn=example_select, run_on_click=True, inputs=[query, code_ref],examples_per_page=10)
|
119 |
+
|
120 |
+
code_ref.render()
|
121 |
+
#with gr.Row():
|
122 |
+
#chat.render()
|
123 |
|
|
|
|
|
124 |
|
125 |
+
|
126 |
with gr.Row():
|
127 |
+
code_llm.render()
|
128 |
+
|
129 |
+
query.submit(submit_query, inputs=[query], outputs=[code_llm])
|
130 |
+
#query.change(submit_query, inputs=[query], outputs=[code_llm])
|
131 |
+
query.change(clean_output, inputs=[], outputs=[code_llm]).then(submit_query, inputs=[query], outputs=[code_llm])
|
132 |
|
133 |
+
demo.launch()
|
|