Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,8 +7,9 @@ import gradio as gr
|
|
| 7 |
warnings.filterwarnings("ignore", category=UserWarning, module="langchain_community")
|
| 8 |
|
| 9 |
from langchain_community.vectorstores import Chroma
|
| 10 |
-
from langchain_huggingface import HuggingFaceEmbeddings
|
| 11 |
-
from
|
|
|
|
| 12 |
from langchain_core.output_parsers import StrOutputParser
|
| 13 |
|
| 14 |
# 1. Fetching the Hugging Face Space security token
|
|
@@ -31,33 +32,31 @@ simplilearn_faq_data = [
|
|
| 31 |
vectorstore = Chroma.from_texts(texts=simplilearn_faq_data, embedding=embeddings)
|
| 32 |
retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k": 2})
|
| 33 |
|
| 34 |
-
# 3. Setup
|
|
|
|
| 35 |
print("Configuring LLM Interface...")
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
temperature=0.3
|
| 42 |
-
do_sample=False,
|
| 43 |
-
huggingfacehub_api_token=hf_token
|
| 44 |
)
|
| 45 |
|
| 46 |
-
# 4. Constructing the Chain using
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
Context:
|
| 54 |
-
{context}</s>
|
| 55 |
-
<|user|>
|
| 56 |
-
{input}</s>
|
| 57 |
-
<|assistant|>
|
| 58 |
-
"""
|
| 59 |
|
| 60 |
-
prompt =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
def format_docs(docs):
|
| 63 |
return "\n\n".join(doc.page_content for doc in docs)
|
|
@@ -85,15 +84,12 @@ def process_chat(message, history):
|
|
| 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
|
| 97 |
)
|
| 98 |
|
| 99 |
# Appending messages via Gradio 6.x specification
|
|
|
|
| 7 |
warnings.filterwarnings("ignore", category=UserWarning, module="langchain_community")
|
| 8 |
|
| 9 |
from langchain_community.vectorstores import Chroma
|
| 10 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 11 |
+
from langchain_openai import ChatOpenAI # Re-introducing this to use HF's OpenAI proxy
|
| 12 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 13 |
from langchain_core.output_parsers import StrOutputParser
|
| 14 |
|
| 15 |
# 1. Fetching the Hugging Face Space security token
|
|
|
|
| 32 |
vectorstore = Chroma.from_texts(texts=simplilearn_faq_data, embedding=embeddings)
|
| 33 |
retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k": 2})
|
| 34 |
|
| 35 |
+
# 3. Setup Open-Source LLM via Hugging Face's OpenAI-Compatible API
|
| 36 |
+
# This completely bypasses the "task: conversational" error by natively using the Messages API
|
| 37 |
print("Configuring LLM Interface...")
|
| 38 |
+
chatModel = ChatOpenAI(
|
| 39 |
+
model="Qwen/Qwen2.5-1.5B-Instruct",
|
| 40 |
+
base_url="https://api-inference.huggingface.co/v1/", # Pointing LangChain directly to HF Servers
|
| 41 |
+
api_key=hf_token,
|
| 42 |
+
max_tokens=500,
|
| 43 |
+
temperature=0.3
|
|
|
|
|
|
|
| 44 |
)
|
| 45 |
|
| 46 |
+
# 4. Constructing the Chain using ChatPromptTemplate (Clean & Native)
|
| 47 |
+
system_prompt = (
|
| 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.\n\n"
|
| 51 |
+
"Context:\n{context}"
|
| 52 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
prompt = ChatPromptTemplate.from_messages(
|
| 55 |
+
[
|
| 56 |
+
("system", system_prompt),
|
| 57 |
+
("human", "{input}"),
|
| 58 |
+
]
|
| 59 |
+
)
|
| 60 |
|
| 61 |
def format_docs(docs):
|
| 62 |
return "\n\n".join(doc.page_content for doc in docs)
|
|
|
|
| 84 |
# Executes the RAG Chain
|
| 85 |
bot_response = rag_chain.invoke({"input": message})
|
| 86 |
|
|
|
|
|
|
|
|
|
|
| 87 |
except Exception as e:
|
| 88 |
# Displays exactly what failed directly on the UI
|
| 89 |
bot_response = (
|
| 90 |
f"⚠️ **Server/API Error Encountered:**\n\n"
|
| 91 |
f"`{str(e)}`\n\n"
|
| 92 |
+
f"**Fix required:** Ensure your `HF_TOKEN` is saved in the Space settings. If it says 'Rate Limit', the free serverless API is temporarily busy."
|
| 93 |
)
|
| 94 |
|
| 95 |
# Appending messages via Gradio 6.x specification
|