Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
|
| 4 |
-
# ✅ LangChain imports
|
| 5 |
from langchain.text_splitter import CharacterTextSplitter
|
| 6 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 7 |
from langchain_community.vectorstores import FAISS
|
|
@@ -9,25 +7,24 @@ from langchain.chains import ConversationalRetrievalChain
|
|
| 9 |
from langchain_community.llms import HuggingFaceHub
|
| 10 |
from langchain_community.document_loaders import PyPDFLoader
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
pdf_path = os.path.join(current_dir, "chimera.pdf")
|
| 15 |
-
loader = PyPDFLoader(pdf_path)
|
| 16 |
documents = loader.load()
|
| 17 |
|
| 18 |
-
#
|
| 19 |
text_splitter = CharacterTextSplitter(chunk_size=800, chunk_overlap=100)
|
| 20 |
texts = text_splitter.split_documents(documents)
|
| 21 |
|
| 22 |
-
#
|
| 23 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 24 |
db = FAISS.from_documents(texts, embeddings)
|
| 25 |
|
| 26 |
-
# --- 4️⃣ Build retriever-based chatbot ---
|
| 27 |
retriever = db.as_retriever(search_kwargs={"k": 3})
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
hf_token = os.
|
|
|
|
|
|
|
| 31 |
|
| 32 |
llm = HuggingFaceHub(
|
| 33 |
repo_id="google/flan-t5-base",
|
|
@@ -40,82 +37,48 @@ qa = ConversationalRetrievalChain.from_llm(
|
|
| 40 |
retriever=retriever
|
| 41 |
)
|
| 42 |
|
| 43 |
-
# --- 6️⃣ Chat history ---
|
| 44 |
chat_history = []
|
| 45 |
|
| 46 |
-
# --- 7️⃣ Respond function ---
|
| 47 |
def respond(message, history):
|
| 48 |
-
history = history[-6:]
|
| 49 |
result = qa({"question": message, "chat_history": history})
|
| 50 |
history.append((message, result["answer"]))
|
| 51 |
return history, history
|
| 52 |
|
| 53 |
-
# --- 8️⃣ Gradio UI with Entry Warning ---
|
| 54 |
with gr.Blocks() as demo:
|
| 55 |
with gr.Column():
|
| 56 |
-
# Warning message
|
| 57 |
warning_text = gr.HTML(
|
| 58 |
-
""
|
| 59 |
-
<div style="background-color:black;color:white;padding:20px;font-family:monospace;font-size:18px;">
|
| 60 |
-
⚠ WARNING — INVESTIGATIVE SIMULATION ⚠<br><br>
|
| 61 |
-
You are about to enter <b>The Chimera Case</b>, a high-stakes investigation into Innovate Future Labs (IFL) and Project Chimera.<br>
|
| 62 |
-
The scenario contains allegations, leaked files, and disputed testimonies. Treat every claim as unverified until verified by evidence.<br>
|
| 63 |
-
Your decisions and observations will guide your understanding of the case.<br><br>
|
| 64 |
-
Are you ready to proceed?
|
| 65 |
-
</div>
|
| 66 |
-
"""
|
| 67 |
)
|
| 68 |
-
|
| 69 |
-
# Buttons
|
| 70 |
enter_btn = gr.Button("Enter the Case")
|
| 71 |
exit_btn = gr.Button("Exit")
|
| 72 |
-
|
| 73 |
-
# Chatbot (hidden initially)
|
| 74 |
chatbot = gr.Chatbot(visible=False)
|
| 75 |
-
user_input = gr.Textbox(placeholder="Type
|
| 76 |
submit_btn = gr.Button("Send", visible=False)
|
| 77 |
|
| 78 |
-
# --- Button interactions ---
|
| 79 |
def enter_case():
|
| 80 |
return (
|
| 81 |
-
gr.update(visible=True),
|
| 82 |
-
gr.update(visible=True),
|
| 83 |
-
gr.update(visible=True),
|
| 84 |
-
gr.update(value=""),
|
| 85 |
-
gr.update(visible=False),
|
| 86 |
-
gr.update(visible=False)
|
| 87 |
)
|
| 88 |
|
| 89 |
def exit_case():
|
| 90 |
return (
|
| 91 |
-
gr.update(value="
|
| 92 |
-
gr.update(visible=False),
|
| 93 |
-
gr.update(visible=False),
|
| 94 |
-
gr.update(visible=False),
|
| 95 |
-
gr.update(visible=False),
|
| 96 |
-
gr.update(visible=False)
|
| 97 |
)
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
inputs=None,
|
| 103 |
-
outputs=[chatbot, user_input, submit_btn, warning_text, enter_btn, exit_btn]
|
| 104 |
-
)
|
| 105 |
|
| 106 |
-
exit_btn.click(
|
| 107 |
-
exit_case,
|
| 108 |
-
inputs=None,
|
| 109 |
-
outputs=[warning_text, chatbot, user_input, submit_btn, enter_btn, exit_btn]
|
| 110 |
-
)
|
| 111 |
-
|
| 112 |
-
submit_btn.click(
|
| 113 |
-
respond,
|
| 114 |
-
inputs=[user_input, chatbot],
|
| 115 |
-
outputs=[chatbot, chatbot]
|
| 116 |
-
)
|
| 117 |
-
|
| 118 |
-
# --- 9️⃣ Launch ---
|
| 119 |
if __name__ == "__main__":
|
| 120 |
demo.launch(share=True, enable_queue=True)
|
| 121 |
-
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
| 3 |
from langchain.text_splitter import CharacterTextSplitter
|
| 4 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 5 |
from langchain_community.vectorstores import FAISS
|
|
|
|
| 7 |
from langchain_community.llms import HuggingFaceHub
|
| 8 |
from langchain_community.document_loaders import PyPDFLoader
|
| 9 |
|
| 10 |
+
# Load PDF
|
| 11 |
+
loader = PyPDFLoader("chimera.pdf")
|
|
|
|
|
|
|
| 12 |
documents = loader.load()
|
| 13 |
|
| 14 |
+
# Split documents
|
| 15 |
text_splitter = CharacterTextSplitter(chunk_size=800, chunk_overlap=100)
|
| 16 |
texts = text_splitter.split_documents(documents)
|
| 17 |
|
| 18 |
+
# Embeddings
|
| 19 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 20 |
db = FAISS.from_documents(texts, embeddings)
|
| 21 |
|
|
|
|
| 22 |
retriever = db.as_retriever(search_kwargs={"k": 3})
|
| 23 |
|
| 24 |
+
# Hugging Face Hub LLM
|
| 25 |
+
hf_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 26 |
+
if hf_token is None:
|
| 27 |
+
raise ValueError("HUGGINGFACEHUB_API_TOKEN not set in Space Secrets!")
|
| 28 |
|
| 29 |
llm = HuggingFaceHub(
|
| 30 |
repo_id="google/flan-t5-base",
|
|
|
|
| 37 |
retriever=retriever
|
| 38 |
)
|
| 39 |
|
|
|
|
| 40 |
chat_history = []
|
| 41 |
|
|
|
|
| 42 |
def respond(message, history):
|
| 43 |
+
history = history[-6:]
|
| 44 |
result = qa({"question": message, "chat_history": history})
|
| 45 |
history.append((message, result["answer"]))
|
| 46 |
return history, history
|
| 47 |
|
|
|
|
| 48 |
with gr.Blocks() as demo:
|
| 49 |
with gr.Column():
|
|
|
|
| 50 |
warning_text = gr.HTML(
|
| 51 |
+
"<div style='background-color:black;color:white;padding:20px;'>⚠ WARNING: Investigative Simulation ⚠<br>Are you ready?</div>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
)
|
|
|
|
|
|
|
| 53 |
enter_btn = gr.Button("Enter the Case")
|
| 54 |
exit_btn = gr.Button("Exit")
|
|
|
|
|
|
|
| 55 |
chatbot = gr.Chatbot(visible=False)
|
| 56 |
+
user_input = gr.Textbox(placeholder="Type here...", visible=False)
|
| 57 |
submit_btn = gr.Button("Send", visible=False)
|
| 58 |
|
|
|
|
| 59 |
def enter_case():
|
| 60 |
return (
|
| 61 |
+
gr.update(visible=True),
|
| 62 |
+
gr.update(visible=True),
|
| 63 |
+
gr.update(visible=True),
|
| 64 |
+
gr.update(value=""),
|
| 65 |
+
gr.update(visible=False),
|
| 66 |
+
gr.update(visible=False)
|
| 67 |
)
|
| 68 |
|
| 69 |
def exit_case():
|
| 70 |
return (
|
| 71 |
+
gr.update(value="Session ended."),
|
| 72 |
+
gr.update(visible=False),
|
| 73 |
+
gr.update(visible=False),
|
| 74 |
+
gr.update(visible=False),
|
| 75 |
+
gr.update(visible=False),
|
| 76 |
+
gr.update(visible=False)
|
| 77 |
)
|
| 78 |
|
| 79 |
+
enter_btn.click(enter_case, inputs=None, outputs=[chatbot, user_input, submit_btn, warning_text, enter_btn, exit_btn])
|
| 80 |
+
exit_btn.click(exit_case, inputs=None, outputs=[warning_text, chatbot, user_input, submit_btn, enter_btn, exit_btn])
|
| 81 |
+
submit_btn.click(respond, inputs=[user_input, chatbot], outputs=[chatbot, chatbot])
|
|
|
|
|
|
|
|
|
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
if __name__ == "__main__":
|
| 84 |
demo.launch(share=True, enable_queue=True)
|
|
|