Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,51 @@
|
|
1 |
-
import
|
2 |
import gradio as gr
|
3 |
-
from transformers import
|
4 |
-
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
inputs = tokenizer(
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
outputs = model.generate(**inputs, **gen_params)
|
28 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
1 |
+
import json
|
2 |
import gradio as gr
|
3 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
4 |
+
|
5 |
+
# 1. Datu ielāde
|
6 |
+
with open("data.json", "r", encoding="utf-8") as f:
|
7 |
+
data = json.load(f)
|
8 |
+
|
9 |
+
# 2. Modeļa un tokenizer ielāde
|
10 |
+
model_name = "google/mt5-small"
|
11 |
+
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
12 |
+
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
13 |
+
|
14 |
+
# 3. Atbildes ģenerēšanas funkcija
|
15 |
+
def generate_answer(question):
|
16 |
+
inputs = tokenizer(question, return_tensors="pt", max_length=512, truncation=True)
|
17 |
+
outputs = model.generate(**inputs, max_new_tokens=200)
|
18 |
+
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
19 |
+
|
20 |
+
#meklē atbilde datu kopā.
|
21 |
+
found_answer=None;
|
22 |
+
for item in data:
|
23 |
+
if question.lower() in item['question'].lower():
|
24 |
+
found_answer=item['answer'];
|
25 |
+
break;
|
26 |
+
|
27 |
+
if found_answer:
|
28 |
+
return found_answer
|
29 |
+
else:
|
30 |
+
return answer
|
31 |
+
|
32 |
+
# 4. Gradio lietotāja saskarne
|
33 |
+
iface = gr.Interface(
|
34 |
+
fn=generate_answer,
|
35 |
+
inputs=gr.Textbox(lines=2, placeholder="Uzdot jautājumu par enerģētiku vai elektromontāžu..."),
|
36 |
+
outputs="text",
|
37 |
+
title="Enerģētikas/Elektromontāžas Čata Robots",
|
38 |
+
description="Uzdot jautājumus par elektromontāžu, vai enerģētiku. Ja ir jautajumi datu bāzē, atbildēs no datu bāzes, pretējā gadījumā, no mT5 modeļa.",
|
39 |
+
)
|
40 |
+
|
41 |
+
iface.launch()
|
42 |
+
|
43 |
+
# data.json piemērs:
|
44 |
+
# [
|
45 |
+
# {"question": "Kādi ir elektroinstalācijas drošības noteikumi?", "answer": "Galvenie noteikumi ir..."},
|
46 |
+
# {"question": "Kā aprēķināt jaudu trīsfāžu ķēdē?", "answer": "Jauda trīsfāžu ķēdē tiek aprēķināta..."},
|
47 |
+
# ...
|
48 |
+
# ]
|
49 |
|
50 |
outputs = model.generate(**inputs, **gen_params)
|
51 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|