File size: 4,422 Bytes
29cfa22
 
110e010
11be1b1
4df7980
b84491a
110e010
 
 
 
b84491a
 
 
 
0d3044e
e374a28
110e010
11be1b1
110e010
0d3044e
29cfa22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110e010
 
 
 
 
 
 
29cfa22
110e010
29cfa22
 
 
 
 
 
 
110e010
 
 
 
 
 
 
 
29cfa22
 
 
110e010
29cfa22
 
 
 
 
 
 
 
 
 
 
 
 
 
5768cee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110e010
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import gradio as gr
import random
import torch
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
from ctransformers import AutoModelForCausalLM
import time
import pickle

# Load LLM (4-bit quantized)
start_load = time.time()
llm = AutoModelForCausalLM.from_pretrained(
    "TheBloke/zephyr-7B-alpha-GGUF",
    model_file="zephyr-7b-alpha.Q4_K_M.gguf",
    model_type="mistral"
)
print(f"Model loaded in {time.time() - start_load:.2f} seconds")

# Load sentences for random queries
with open("bc sentences.pkl", "rb") as f:
    bs_sen = pickle.load(f)

# Load embedding model
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")

# Load FAISS indexes
faiss_a = FAISS.load_local("faiss/index_a", embeddings=embedding_model, allow_dangerous_deserialization=True)
faiss_b = FAISS.load_local("faiss/index_b", embeddings=embedding_model, allow_dangerous_deserialization=True)

# Set up retrievers
retriever_a = faiss_a.as_retriever(search_kwargs={"k": 10})
retriever_b = faiss_b.as_retriever(search_kwargs={"k": 10})

# System prompts
guidance_a = """You are a 1920s trade union representative.
Use the retrieved sentences as your knowledge base.
Speak persuasively as if you are arguing with a fellow trade unionist.
Do not use your own knowledge.
Vary your sentence structures, do not repeat phrases.
Respond with 2 sentences maximum.
Do not use the names of any trade union members or name any geographic locations."""

guidance_b = """You are a 2020s trade union representative.
Use the retrieved sentences as your knowledge base.
Speak persuasively as if you are arguing with a fellow trade unionist.
Do not use your own knowledge.
Vary your sentence structures, do not repeat phrases.
Respond with 2 sentences maximum.
Do not use the names of any trade union members or name any geographic locations."""

def generate_text(prompt):
    try:
        output = llm(prompt, max_new_tokens=150, temperature=0.7, do_sample=True)
        return output.strip()
    except Exception as e:
        return f"[Error generating response: {e}]"

# Dialogue turn generator
def generate_turn(query, retriever, prompt_text):
    docs = retriever.get_relevant_documents(query)
    context = "\n".join(doc.page_content for doc in docs)
    prompt = f"{prompt_text}\n\nYou just heard the following message:\n\"{query}\"\n\nHere are 5 excerpts from your documents that may help you reply:\n{context}\n\nRespond to the message above based ONLY on this information, and speak as if you were in a real conversation."
    return generate_text(prompt)

# Conversation function
def simulate_conversation(query, history):
    if not query.strip():
        return "", history

    a_reply = generate_turn(query, retriever_a, guidance_a)
    b_reply = generate_turn(a_reply, retriever_b, guidance_b)

    new_history = history + [(f"User: {query}", f"Speaker A: {a_reply}\nSpeaker B: {b_reply}")]
    return "", new_history

# Random prompt from bs_sen
def random_query(history):
    query = random.choice(bs_sen)
    return simulate_conversation(query, history)

# Gradio UI
with gr.Blocks() as demo:
    gr.Markdown("## Dialogue Simulator between Two Union Eras")
    chatbot = gr.Chatbot()
    txt = gr.Textbox(label="Ask a question")
    ask_btn = gr.Button("Ask")
    rand_btn = gr.Button("Random Conversation")
    state = gr.State([])

    ask_btn.click(fn=simulate_conversation, inputs=[txt, state], outputs=[txt, chatbot])
    rand_btn.click(fn=random_query, inputs=state, outputs=[txt, chatbot])

# AFTER LOADING FAISS
try:
    test_doc = faiss_a.similarity_search("test", k=1)
    print("FAISS A loaded successfully:", test_doc)
except Exception as e:
    print("[ERROR] FAISS A failed to load:", str(e))

try:
    test_doc = faiss_b.similarity_search("test", k=1)
    print("FAISS B loaded successfully:", test_doc)
except Exception as e:
    print("[ERROR] FAISS B failed to load:", str(e))

# AFTER LOADING PKL
try:
    print(f"Loaded {len(bs_sen)} sentences from bc_sentences.pkl")
    print("Sample sentence:", random.choice(bs_sen))
except Exception as e:
    print("[ERROR] bc_sentences.pkl failed to load:", str(e))

# AFTER LOADING LLM
try:
    warmup_response = llm("Hello", max_new_tokens=10)
    print("LLM warmup response:", warmup_response)
except Exception as e:
    print("[ERROR] LLM failed on warmup:", str(e))

demo.launch()