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|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import time
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
from main import (
|
| 7 |
+
load_model_with_questions_and_answers,
|
| 8 |
+
simulate_question_answering,
|
| 9 |
+
find_similar_question
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
# Deaktiviere alle Logging-Ausgaben außer kritischen Fehlern
|
| 13 |
+
logging.getLogger().setLevel(logging.CRITICAL)
|
| 14 |
+
|
| 15 |
+
# Modell laden
|
| 16 |
+
category_nodes, questions = load_model_with_questions_and_answers("model_with_qa.json")
|
| 17 |
+
|
| 18 |
+
def test_model_with_answers(category_nodes, questions, query):
|
| 19 |
+
"""
|
| 20 |
+
Testet das Modell mit einer Abfrage und gibt die gefundene Frage, Antwort und Gewichtung zurück.
|
| 21 |
+
|
| 22 |
+
Args:
|
| 23 |
+
category_nodes (list): Liste der Kategorie-Knoten.
|
| 24 |
+
questions (list): Liste der Fragen.
|
| 25 |
+
query (str): Die Abfrage, nach der gesucht werden soll.
|
| 26 |
+
|
| 27 |
+
Returns:
|
| 28 |
+
tuple: Die gefundene Frage, Antwort und Gewichtung.
|
| 29 |
+
"""
|
| 30 |
+
# Suche nach der ähnlichsten Frage im Modell
|
| 31 |
+
matched_question = find_similar_question(questions, query)
|
| 32 |
+
|
| 33 |
+
if matched_question and matched_question.get('question') != "Keine passende Frage gefunden":
|
| 34 |
+
answer = matched_question.get('answer', 'Keine Antwort verfügbar')
|
| 35 |
+
|
| 36 |
+
# Simulation der Fragebeantwortung (Gewichtung/Aktivierung)
|
| 37 |
+
activation = simulate_question_answering(category_nodes, matched_question['question'], questions)
|
| 38 |
+
|
| 39 |
+
# Rückgabe der relevanten Informationen
|
| 40 |
+
return f"Frage: \"{query}\"", f"Antwort: \"{answer}\"", f"Gewichtung: {activation:.2f}"
|
| 41 |
+
else:
|
| 42 |
+
# Falls keine passende Frage gefunden wurde
|
| 43 |
+
return f"Frage: \"{query}\"", "Antwort: \"Keine passende Frage gefunden\"", "Gewichtung: 0.00"
|
| 44 |
+
|
| 45 |
+
def measure_response_time(func, *args, **kwargs):
|
| 46 |
+
"""
|
| 47 |
+
Misst die Zeit, die eine Funktion benötigt, um ausgeführt zu werden, und gibt die Ergebnisse zusammen mit der Zeit zurück.
|
| 48 |
+
|
| 49 |
+
Args:
|
| 50 |
+
func (callable): Die auszuführende Funktion.
|
| 51 |
+
*args: Positionsargumente für die Funktion.
|
| 52 |
+
**kwargs: Schlüsselwortargumente für die Funktion.
|
| 53 |
+
|
| 54 |
+
Returns:
|
| 55 |
+
tuple: Die Ergebnisse der Funktion und die verstrichene Zeit in Millisekunden.
|
| 56 |
+
"""
|
| 57 |
+
start_time = time.time()
|
| 58 |
+
result = func(*args, **kwargs)
|
| 59 |
+
end_time = time.time()
|
| 60 |
+
elapsed_time = (end_time - start_time) * 1000 # Umwandlung in Millisekunden
|
| 61 |
+
return result, elapsed_time
|
| 62 |
+
|
| 63 |
+
def extract_questions_and_answers_from_json(input_json, output_txt):
|
| 64 |
+
"""
|
| 65 |
+
Extrahiert Fragen und Antworten aus einer JSON-Datei und schreibt sie in eine Textdatei.
|
| 66 |
+
|
| 67 |
+
Args:
|
| 68 |
+
input_json (str): Der Pfad zur Eingabe-JSON-Datei.
|
| 69 |
+
output_txt (str): Der Pfad zur Ausgabe-Textdatei.
|
| 70 |
+
"""
|
| 71 |
+
try:
|
| 72 |
+
with open(input_json, mode='r', encoding='utf-8') as jsonfile, open(output_txt, mode='w', encoding='utf-8') as txtfile:
|
| 73 |
+
data = json.load(jsonfile)
|
| 74 |
+
questions = data.get('questions', [])
|
| 75 |
+
for question in questions:
|
| 76 |
+
q = question.get('question', '')
|
| 77 |
+
a = question.get('answer', '')
|
| 78 |
+
if q and a:
|
| 79 |
+
txtfile.write(f'"question": "{q}",\n')
|
| 80 |
+
txtfile.write(f'"answer": "{a}"\n\n')
|
| 81 |
+
print(f"Fragen und Antworten wurden erfolgreich in {output_txt} geschrieben.")
|
| 82 |
+
except FileNotFoundError:
|
| 83 |
+
print(f"Die Datei {input_json} wurde nicht gefunden.")
|
| 84 |
+
except json.JSONDecodeError:
|
| 85 |
+
print(f"Fehler beim Parsen der JSON-Datei {input_json}.")
|
| 86 |
+
except Exception as e:
|
| 87 |
+
print(f"Ein Fehler ist aufgetreten: {e}")
|
| 88 |
+
|
| 89 |
+
def load_questions_and_answers(file_path):
|
| 90 |
+
"""
|
| 91 |
+
Lädt Fragen und Antworten aus einer Textdatei.
|
| 92 |
+
|
| 93 |
+
Args:
|
| 94 |
+
file_path (str): Der Pfad zur Textdatei.
|
| 95 |
+
|
| 96 |
+
Returns:
|
| 97 |
+
str: Der Inhalt der Textdatei.
|
| 98 |
+
"""
|
| 99 |
+
try:
|
| 100 |
+
with open(file_path, mode='r', encoding='utf-8') as file:
|
| 101 |
+
content = file.read()
|
| 102 |
+
return content
|
| 103 |
+
except FileNotFoundError:
|
| 104 |
+
return "Datei nicht gefunden."
|
| 105 |
+
except Exception as e:
|
| 106 |
+
return f"Fehler beim Lesen der Datei: {e}"
|
| 107 |
+
|
| 108 |
+
# Gradio-Interface
|
| 109 |
+
def gradio_interface(query):
|
| 110 |
+
"""
|
| 111 |
+
Gradio-Schnittstelle zur Verarbeitung der Benutzerabfrage.
|
| 112 |
+
|
| 113 |
+
Args:
|
| 114 |
+
query (str): Die Abfrage des Benutzers.
|
| 115 |
+
|
| 116 |
+
Returns:
|
| 117 |
+
tuple: Die gefundene Frage, Antwort, Gewichtung und die verstrichene Zeit in Millisekunden.
|
| 118 |
+
"""
|
| 119 |
+
if category_nodes and questions:
|
| 120 |
+
result, elapsed_time = measure_response_time(test_model_with_answers, category_nodes, questions, query)
|
| 121 |
+
return *result, f"Reaktionszeit: {elapsed_time:.2f} ms"
|
| 122 |
+
else:
|
| 123 |
+
logging.critical("Kein Modell gefunden.")
|
| 124 |
+
return "Fehler", "Kein Modell geladen.", "0.00", "Reaktionszeit: 0.00 ms"
|
| 125 |
+
|
| 126 |
+
# Pfade zu den Dateien
|
| 127 |
+
input_json = 'model_with_qa.json'
|
| 128 |
+
output_txt = 'questions_and_answers.txt'
|
| 129 |
+
|
| 130 |
+
# Extrahiere Fragen und Antworten aus der JSON-Datei und speichere sie in der Textdatei
|
| 131 |
+
extract_questions_and_answers_from_json(input_json, output_txt)
|
| 132 |
+
|
| 133 |
+
# Lade die Fragen und Antworten aus der Textdatei
|
| 134 |
+
questions_and_answers_content = load_questions_and_answers(output_txt)
|
| 135 |
+
|
| 136 |
+
# Erstelle das Gradio-Interface
|
| 137 |
+
iface = gr.Interface(
|
| 138 |
+
fn=gradio_interface,
|
| 139 |
+
inputs=gr.Textbox(label="Frage eingeben", placeholder="Stellen Sie eine Frage..."),
|
| 140 |
+
outputs=[
|
| 141 |
+
gr.Textbox(label="Frage"),
|
| 142 |
+
gr.Textbox(label="Antwort"),
|
| 143 |
+
gr.Textbox(label="Gewichtung"),
|
| 144 |
+
gr.Textbox(label="Reaktionszeit")
|
| 145 |
+
],
|
| 146 |
+
title="Frage-Antwort-Modell",
|
| 147 |
+
description="Stellen Sie eine Frage, und das Modell wird versuchen, eine passende Antwort mit Gewichtung zu finden."
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
# Füge ein aufklappbares Ausgabefenster hinzu, das die Liste der Fragen und Antworten anzeigt
|
| 151 |
+
with gr.Blocks() as demo:
|
| 152 |
+
gr.Markdown("## Frage-Antwort-Modell")
|
| 153 |
+
with gr.Row():
|
| 154 |
+
with gr.Column():
|
| 155 |
+
iface.render()
|
| 156 |
+
with gr.Column():
|
| 157 |
+
gr.Markdown("### Fragen und Antworten")
|
| 158 |
+
gr.Textbox(value=questions_and_answers_content, lines=20, label="Fragen und Antworten", interactive=False)
|
| 159 |
+
|
| 160 |
+
# Starte das Gradio-Interface
|
| 161 |
+
demo.launch()
|
model_with_qa.json
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fb04d6f14ef6c9547b17ea50603b0a0d9acfa244ac0c8cdcaf751d5365fff7b1
|
| 3 |
+
size 18533582
|