Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,43 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
# --- Load multiple manuals ---
|
| 4 |
manuals = {}
|
| 5 |
|
| 6 |
-
for filename in ["
|
| 7 |
try:
|
| 8 |
with open(filename, "r", encoding="utf-8") as f:
|
| 9 |
manuals[filename] = f.read().lower()
|
| 10 |
except FileNotFoundError:
|
| 11 |
manuals[filename] = ""
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
found = []
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
if found:
|
| 22 |
-
reply = "Here’s what I found
|
| 23 |
else:
|
| 24 |
-
reply = "I couldn’t find that in
|
| 25 |
|
| 26 |
messages.append(("assistant", reply))
|
| 27 |
return messages
|
| 28 |
|
| 29 |
with gr.Blocks() as demo:
|
| 30 |
chatbot_ui = gr.Chatbot(type="messages")
|
| 31 |
-
msg = gr.Textbox(placeholder="Ask about your Sierra…")
|
| 32 |
clear = gr.Button("Clear")
|
| 33 |
|
| 34 |
def respond(message, chat_history):
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
# --- Load multiple manuals into a dictionary ---
|
| 4 |
manuals = {}
|
| 5 |
|
| 6 |
+
for filename in ["Sierra.txt", "sierra_engine.txt"]:
|
| 7 |
try:
|
| 8 |
with open(filename, "r", encoding="utf-8") as f:
|
| 9 |
manuals[filename] = f.read().lower()
|
| 10 |
except FileNotFoundError:
|
| 11 |
manuals[filename] = ""
|
| 12 |
|
| 13 |
+
messages = []
|
| 14 |
+
|
| 15 |
+
def chatbot(user_input):
|
| 16 |
+
global messages
|
| 17 |
+
messages.append(("user", user_input))
|
| 18 |
+
|
| 19 |
found = []
|
| 20 |
+
query_words = user_input.lower().split()
|
| 21 |
+
|
| 22 |
+
for name, text in manuals.items():
|
| 23 |
+
for word in query_words:
|
| 24 |
+
if word in text:
|
| 25 |
+
start = text.find(word)
|
| 26 |
+
snippet = text[start:start+400] # grab 400 characters around the match
|
| 27 |
+
found.append(f"From {name}:\n{snippet}")
|
| 28 |
+
break # stop after first match per file
|
| 29 |
|
| 30 |
if found:
|
| 31 |
+
reply = "Here’s what I found:\n\n" + "\n---\n".join(found[:3])
|
| 32 |
else:
|
| 33 |
+
reply = "I couldn’t find that in Sierra or engine manuals."
|
| 34 |
|
| 35 |
messages.append(("assistant", reply))
|
| 36 |
return messages
|
| 37 |
|
| 38 |
with gr.Blocks() as demo:
|
| 39 |
chatbot_ui = gr.Chatbot(type="messages")
|
| 40 |
+
msg = gr.Textbox(placeholder="Ask about your Sierra or engine manual…")
|
| 41 |
clear = gr.Button("Clear")
|
| 42 |
|
| 43 |
def respond(message, chat_history):
|