|
|
import gradio as gr |
|
|
|
|
|
|
|
|
manuals = {} |
|
|
|
|
|
for filename in ["Sierra.txt", "sierra_engine.txt"]: |
|
|
try: |
|
|
with open(filename, "r", encoding="utf-8") as f: |
|
|
manuals[filename] = f.read().lower() |
|
|
except FileNotFoundError: |
|
|
manuals[filename] = "" |
|
|
|
|
|
messages = [] |
|
|
|
|
|
def chatbot(user_input): |
|
|
global messages |
|
|
|
|
|
messages.append({"role": "user", "content": user_input}) |
|
|
|
|
|
found = [] |
|
|
query_words = user_input.lower().split() |
|
|
|
|
|
for name, text in manuals.items(): |
|
|
for word in query_words: |
|
|
if word in text: |
|
|
start = text.find(word) |
|
|
snippet = text[start:start+400] |
|
|
found.append(f"From {name}:\n{snippet}") |
|
|
break |
|
|
|
|
|
if found: |
|
|
reply = "Here’s what I found:\n\n" + "\n---\n".join(found[:3]) |
|
|
else: |
|
|
reply = "I couldn’t find that in Sierra or engine manuals." |
|
|
|
|
|
|
|
|
messages.append({"role": "assistant", "content": reply}) |
|
|
return messages |
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
chatbot_ui = gr.Chatbot(type="messages") |
|
|
msg = gr.Textbox(placeholder="Ask about your Sierra or engine manual…") |
|
|
clear = gr.Button("Clear") |
|
|
|
|
|
def respond(message, chat_history): |
|
|
bot_reply = chatbot(message) |
|
|
return bot_reply, "" |
|
|
|
|
|
msg.submit(respond, [msg, chatbot_ui], [chatbot_ui, msg]) |
|
|
clear.click(lambda: [], None, chatbot_ui, queue=False) |
|
|
|
|
|
demo.launch() |