import gradio as gr # --- Load multiple manuals into a dictionary --- 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 # Add user message in new format 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] # grab 400 characters around the match found.append(f"From {name}:\n{snippet}") break # stop after first match per file 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." # Add assistant message in new format 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()