Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,11 +11,11 @@ embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
|
| 11 |
vectorstore = FAISS.load_local("learncpp_faiss_index_full", embeddings, allow_dangerous_deserialization=True)
|
| 12 |
retriever = vectorstore.as_retriever(search_kwargs={"k": 5})
|
| 13 |
|
| 14 |
-
# 2. Set up the LLM
|
| 15 |
hf_token = os.environ.get("HF_TOKEN")
|
| 16 |
llm = HuggingFaceEndpoint(
|
| 17 |
-
repo_id="
|
| 18 |
-
|
| 19 |
max_new_tokens=512,
|
| 20 |
temperature=0.1,
|
| 21 |
huggingfacehub_api_token=hf_token
|
|
@@ -46,14 +46,20 @@ rag_chain = (
|
|
| 46 |
| StrOutputParser()
|
| 47 |
)
|
| 48 |
|
| 49 |
-
# 4. Define the Chat Interface
|
| 50 |
def chat_function(message, history):
|
| 51 |
try:
|
| 52 |
formatted_input = ""
|
| 53 |
if history:
|
| 54 |
formatted_input += "Previous Conversation Context:\n"
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
formatted_input += "\n"
|
| 58 |
|
| 59 |
formatted_input += f"Current Question: {message}"
|
|
@@ -68,8 +74,7 @@ demo = gr.ChatInterface(
|
|
| 68 |
fn=chat_function,
|
| 69 |
title="LearnCpp.com AI Assistant",
|
| 70 |
description="Ask me any C++ question! I retrieve my answers directly from the complete LearnCpp tutorials.",
|
| 71 |
-
examples=["What is a pointer?", "Explain dynamic memory allocation.", "Give me an example of the previous concept."]
|
| 72 |
-
type="tuples" # ADDED THIS: Forces Gradio to use the format our memory loop expects!
|
| 73 |
)
|
| 74 |
|
| 75 |
if __name__ == "__main__":
|
|
|
|
| 11 |
vectorstore = FAISS.load_local("learncpp_faiss_index_full", embeddings, allow_dangerous_deserialization=True)
|
| 12 |
retriever = vectorstore.as_retriever(search_kwargs={"k": 5})
|
| 13 |
|
| 14 |
+
# 2. Set up the LLM (Upgraded to Qwen Coder!)
|
| 15 |
hf_token = os.environ.get("HF_TOKEN")
|
| 16 |
llm = HuggingFaceEndpoint(
|
| 17 |
+
repo_id="Qwen/Qwen2.5-Coder-7B-Instruct",
|
| 18 |
+
task="text-generation",
|
| 19 |
max_new_tokens=512,
|
| 20 |
temperature=0.1,
|
| 21 |
huggingfacehub_api_token=hf_token
|
|
|
|
| 46 |
| StrOutputParser()
|
| 47 |
)
|
| 48 |
|
| 49 |
+
# 4. Define the Chat Interface (Bulletproof Memory)
|
| 50 |
def chat_function(message, history):
|
| 51 |
try:
|
| 52 |
formatted_input = ""
|
| 53 |
if history:
|
| 54 |
formatted_input += "Previous Conversation Context:\n"
|
| 55 |
+
# Natively parses Gradio 6's new dictionary memory format
|
| 56 |
+
for msg in history:
|
| 57 |
+
if isinstance(msg, dict):
|
| 58 |
+
role = "User" if msg.get("role") == "user" else "Bot"
|
| 59 |
+
formatted_input += f"{role}: {msg.get('content', '')}\n"
|
| 60 |
+
else:
|
| 61 |
+
# Fallback just in case it behaves weirdly
|
| 62 |
+
formatted_input += f"User: {msg[0]}\nBot: {msg[1]}\n"
|
| 63 |
formatted_input += "\n"
|
| 64 |
|
| 65 |
formatted_input += f"Current Question: {message}"
|
|
|
|
| 74 |
fn=chat_function,
|
| 75 |
title="LearnCpp.com AI Assistant",
|
| 76 |
description="Ask me any C++ question! I retrieve my answers directly from the complete LearnCpp tutorials.",
|
| 77 |
+
examples=["What is a pointer?", "Explain dynamic memory allocation.", "Give me an example of the previous concept."]
|
|
|
|
| 78 |
)
|
| 79 |
|
| 80 |
if __name__ == "__main__":
|