Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,8 +33,9 @@ retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k
|
|
| 33 |
|
| 34 |
# 3. Setup Low Parameter Open-Source LLM Generator via Serverless API
|
| 35 |
print("Configuring LLM Interface...")
|
|
|
|
| 36 |
chatModel = HuggingFaceEndpoint(
|
| 37 |
-
repo_id="
|
| 38 |
task="text-generation",
|
| 39 |
max_new_tokens=500,
|
| 40 |
temperature=0.3,
|
|
@@ -42,24 +43,22 @@ chatModel = HuggingFaceEndpoint(
|
|
| 42 |
huggingfacehub_api_token=hf_token
|
| 43 |
)
|
| 44 |
|
| 45 |
-
# 4. Constructing the Chain using standard PromptTemplate
|
| 46 |
-
#
|
| 47 |
-
template = """<|
|
| 48 |
You are the Simplilearn MOOC Support Agent, an advanced, polite, and professional AI learning assistant named Lane.
|
| 49 |
Answer the student's inquiry accurately using ONLY the retrieved context below.
|
| 50 |
If the answer cannot be found in the context, politely inform them that you only handle Simplilearn academic protocols and advise them to contact support@simplilearn.com.
|
| 51 |
|
| 52 |
Context:
|
| 53 |
-
{context}<
|
| 54 |
-
<|
|
| 55 |
-
{input}<
|
| 56 |
-
<|
|
| 57 |
"""
|
| 58 |
|
| 59 |
-
# Switched from ChatPromptTemplate to standard PromptTemplate
|
| 60 |
prompt = PromptTemplate.from_template(template)
|
| 61 |
|
| 62 |
-
# Helper function to combine document chunks
|
| 63 |
def format_docs(docs):
|
| 64 |
return "\n\n".join(doc.page_content for doc in docs)
|
| 65 |
|
|
@@ -86,16 +85,15 @@ def process_chat(message, history):
|
|
| 86 |
# Executes the RAG Chain
|
| 87 |
bot_response = rag_chain.invoke({"input": message})
|
| 88 |
|
| 89 |
-
# Clean up
|
| 90 |
-
bot_response = bot_response.replace("<
|
| 91 |
|
| 92 |
except Exception as e:
|
| 93 |
-
#
|
| 94 |
bot_response = (
|
| 95 |
f"⚠️ **Server/API Error Encountered:**\n\n"
|
| 96 |
f"`{str(e)}`\n\n"
|
| 97 |
-
f"**Fix required:**
|
| 98 |
-
f"and add a New Secret named `HF_TOKEN` containing your Hugging Face Access Token."
|
| 99 |
)
|
| 100 |
|
| 101 |
# Appending messages via Gradio 6.x specification
|
|
|
|
| 33 |
|
| 34 |
# 3. Setup Low Parameter Open-Source LLM Generator via Serverless API
|
| 35 |
print("Configuring LLM Interface...")
|
| 36 |
+
# Switched to TinyLlama: A 1.1B model that natively supports text-generation and runs perfectly on HF Serverless
|
| 37 |
chatModel = HuggingFaceEndpoint(
|
| 38 |
+
repo_id="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
|
| 39 |
task="text-generation",
|
| 40 |
max_new_tokens=500,
|
| 41 |
temperature=0.3,
|
|
|
|
| 43 |
huggingfacehub_api_token=hf_token
|
| 44 |
)
|
| 45 |
|
| 46 |
+
# 4. Constructing the Chain using standard PromptTemplate
|
| 47 |
+
# Formatted explicitly for TinyLlama's token requirements to prevent hallucinations
|
| 48 |
+
template = """<|system|>
|
| 49 |
You are the Simplilearn MOOC Support Agent, an advanced, polite, and professional AI learning assistant named Lane.
|
| 50 |
Answer the student's inquiry accurately using ONLY the retrieved context below.
|
| 51 |
If the answer cannot be found in the context, politely inform them that you only handle Simplilearn academic protocols and advise them to contact support@simplilearn.com.
|
| 52 |
|
| 53 |
Context:
|
| 54 |
+
{context}</s>
|
| 55 |
+
<|user|>
|
| 56 |
+
{input}</s>
|
| 57 |
+
<|assistant|>
|
| 58 |
"""
|
| 59 |
|
|
|
|
| 60 |
prompt = PromptTemplate.from_template(template)
|
| 61 |
|
|
|
|
| 62 |
def format_docs(docs):
|
| 63 |
return "\n\n".join(doc.page_content for doc in docs)
|
| 64 |
|
|
|
|
| 85 |
# Executes the RAG Chain
|
| 86 |
bot_response = rag_chain.invoke({"input": message})
|
| 87 |
|
| 88 |
+
# Clean up any trailing end-of-sequence tokens if TinyLlama generates them
|
| 89 |
+
bot_response = bot_response.replace("</s>", "").strip()
|
| 90 |
|
| 91 |
except Exception as e:
|
| 92 |
+
# Displays exactly what failed directly on the UI
|
| 93 |
bot_response = (
|
| 94 |
f"⚠️ **Server/API Error Encountered:**\n\n"
|
| 95 |
f"`{str(e)}`\n\n"
|
| 96 |
+
f"**Fix required:** Ensure your `HF_TOKEN` is saved in the Space settings. If the API is temporarily busy, try again in a few moments."
|
|
|
|
| 97 |
)
|
| 98 |
|
| 99 |
# Appending messages via Gradio 6.x specification
|