File size: 1,594 Bytes
75ba1ca
 
d996312
dcb4118
 
d996312
dcb4118
 
 
 
 
49350f7
d996312
 
 
 
2f46ce1
 
d996312
49350f7
d996312
 
 
 
 
 
 
 
 
49350f7
 
d996312
49350f7
d996312
49350f7
2f46ce1
 
75ba1ca
 
 
49350f7
d996312
75ba1ca
 
 
 
 
 
 
2f46ce1
75ba1ca
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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()