VectorV17 commited on
Commit
24e4323
·
verified ·
1 Parent(s): c1428e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +192 -72
app.py CHANGED
@@ -1,86 +1,206 @@
1
- from flask import Flask, render_template, jsonify, request
2
- from src.helper import download_hugging_face_embeddings
3
- from langchain_pinecone import PineconeVectorStore
4
- # Native Hugging Face Chat Model imports
5
- from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
6
- from langchain.chains import create_retrieval_chain
7
- from langchain.chains.combine_documents import create_stuff_documents_chain
8
- from langchain_core.prompts import ChatPromptTemplate
9
- from dotenv import load_dotenv
10
- from src.prompt import *
11
  import os
 
 
 
12
 
13
- app = Flask(__name__)
14
-
15
- load_dotenv()
16
-
17
- # Fetching environment credentials
18
- PINECONE_API_KEY = os.environ.get('PINECONE_API_KEY')
19
- HF_TOKEN = os.environ.get('HF_TOKEN') or os.environ.get('HUGGINGFACEHUB_API_TOKEN')
20
-
21
- # Ensuring keys are accessible in the environment mapping
22
- os.environ["PINECONE_API_KEY"] = PINECONE_API_KEY
23
- os.environ["HUGGINGFACEHUB_API_TOKEN"] = HF_TOKEN
24
 
25
- # 1. Initialize Embeddings and Vector Store
26
- embeddings = download_hugging_face_embeddings()
27
-
28
- index_name = "medical-chatbot"
29
- docsearch = PineconeVectorStore.from_existing_index(
30
- index_name=index_name,
31
- embedding=embeddings
32
- )
33
-
34
- retriever = docsearch.as_retriever(search_type="similarity", search_kwargs={"k": 3})
35
 
36
- # 2. Configure Native Hugging Face LLM Client
37
- # Setting task="conversational" satisfies the backend requirements perfectly.
38
- print("Initializing Hugging Face conversational endpoint...")
39
- llm = HuggingFaceEndpoint(
40
- repo_id="Qwen/Qwen2.5-1.5B-Instruct",
41
- task="conversational",
42
- huggingfacehub_api_token=HF_TOKEN,
43
- max_new_tokens=500,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  temperature=0.3,
45
- timeout=300 # Generous 5-minute timeout window to prevent connection cuts during server spin-ups
46
  )
47
 
48
- # Wrap the endpoint into a Chat Model interface to accept ChatPromptTemplates
49
- chatModel = ChatHuggingFace(llm=llm)
50
-
51
- # 3. Create the Prompt and RAG Chain
52
- prompt = ChatPromptTemplate.from_messages(
53
- [
54
- ("system", system_prompt),
55
- ("human", "{input}"),
56
- ]
57
  )
58
 
59
- question_answer_chain = create_stuff_documents_chain(chatModel, prompt)
60
- rag_chain = create_retrieval_chain(retriever, question_answer_chain)
61
-
62
-
63
- @app.route("/")
64
- def index():
65
- return render_template('chat.html')
66
-
 
 
 
 
 
 
 
 
 
67
 
68
- @app.route("/get", methods=["GET", "POST"])
69
- def chat():
70
- msg = request.form["msg"]
71
- print(f"User Message: {msg}")
 
 
72
 
 
 
 
73
  try:
74
- # Invoke the complete RAG pipeline
75
- response = rag_chain.invoke({"input": msg})
76
- print("Response : ", response["answer"])
77
- return str(response["answer"])
78
 
79
  except Exception as e:
80
- print(f"Execution Error: {str(e)}")
81
- # Graceful UI response to catch any temporary server overloads
82
- return "⚠️ The Hugging Face serverless hub is currently busy or spinning up the model instance. Please submit your message once more."
83
-
84
-
85
- if __name__ == '__main__':
86
- app.run(host="0.0.0.0", port=8080, debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
+ import datetime
3
+ import warnings
4
+ import gradio as gr
5
 
6
+ # Suppress deprecation warnings for a cleaner console
7
+ warnings.filterwarnings("ignore", category=UserWarning, module="langchain_community")
 
 
 
 
 
 
 
 
 
8
 
9
+ from langchain_chroma import Chroma
10
+ from langchain_huggingface import HuggingFaceEmbeddings
11
+ from langchain_openai import ChatOpenAI
12
+ from langchain_core.prompts import ChatPromptTemplate
13
+ from langchain_core.output_parsers import StrOutputParser
14
+ from dotenv import load_dotenv
 
 
 
 
15
 
16
+ # Load Environment Variables
17
+ load_dotenv()
18
+ hf_token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACEHUB_API_TOKEN")
19
+
20
+ # -------------------------------------------------------------------------
21
+ # 1. Setup Vector Store & Embeddings Pipeline
22
+ # -------------------------------------------------------------------------
23
+ print("Loading semantic embedding vectors...")
24
+ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
25
+
26
+ simplilearn_faq_data = [
27
+ "Simplilearn Refund Policy: Learners are entitled to a full 100% refund within 7 days of course purchase, provided they have not accessed more than 25% of the unlocked asynchronous content or attended any live virtual cohort sessions.",
28
+ "Professional Certification Requirements: To unlock your official Simplilearn digital certificate, you must achieve a minimum score of 80% in the final course assessment and successfully submit all mandatory course projects.",
29
+ "Live Virtual Class Attendance: If a student misses an interactive live session, high-definition cloud recordings are automatically processed and made available on the student LMS dashboard within 24 hours.",
30
+ "Technical Support Availability: Technical assistance for cloud labs, environment resets, or workspace access is available 24/7. Students can raise a ticket via the 'Help and Support' tab or email support@simplilearn.com.",
31
+ "Data Science Bootcamp Prerequisites: This flagship program spans 6 months. It requires basic exposure to high-school level mathematics and rudimentary programming logic (Python familiarity is highly recommended but not mandatory)."
32
+ ]
33
+
34
+ vectorstore = Chroma.from_texts(texts=simplilearn_faq_data, embedding=embeddings)
35
+ retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k": 2})
36
+
37
+ # -------------------------------------------------------------------------
38
+ # 2. Setup Robust LLM Interface via OpenAI-Compatible Endpoint
39
+ # -------------------------------------------------------------------------
40
+ print("Configuring LLM Interface...")
41
+ chatModel = ChatOpenAI(
42
+ model="Qwen/Qwen2.5-1.5B-Instruct",
43
+ base_url="https://api-inference.huggingface.co/models/Qwen/Qwen2.5-1.5B-Instruct/v1/",
44
+ api_key=hf_token,
45
+ max_tokens=400,
46
  temperature=0.3,
47
+ max_retries=2 # Helps prevent timeout crashes during model wake-up
48
  )
49
 
50
+ system_prompt = (
51
+ "You are the Simplilearn MOOC Support Agent, an advanced, polite, and professional AI learning assistant named Lane. "
52
+ "Answer the student's inquiry accurately using ONLY the retrieved context below. "
53
+ "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"
54
+ "Context:\n{context}"
 
 
 
 
55
  )
56
 
57
+ prompt = ChatPromptTemplate.from_messages([
58
+ ("system", system_prompt),
59
+ ("human", "{input}"),
60
+ ])
61
+
62
+ def format_docs(docs):
63
+ return "\n\n".join(doc.page_content for doc in docs)
64
+
65
+ rag_chain = (
66
+ {
67
+ "context": lambda x: format_docs(retriever.invoke(x["input"])),
68
+ "input": lambda x: x["input"]
69
+ }
70
+ | prompt
71
+ | chatModel
72
+ | StrOutputParser()
73
+ )
74
 
75
+ # -------------------------------------------------------------------------
76
+ # 3. Application Core Logic
77
+ # -------------------------------------------------------------------------
78
+ def process_chat(message, history):
79
+ if not message.strip():
80
+ return "", history
81
 
82
+ if history is None:
83
+ history = []
84
+
85
  try:
86
+ # Executes the RAG Chain
87
+ bot_response = rag_chain.invoke({"input": message})
 
 
88
 
89
  except Exception as e:
90
+ # Gracefully handle HF timeout/wake-up errors instead of crashing the UI
91
+ bot_response = (
92
+ f"⚠️ **Connection Status:**\n\n"
93
+ f"`{str(e)}`\n\n"
94
+ f"**Note:** The Hugging Face inference model is currently waking up. This takes about 30-60 seconds. Please wait a moment and try sending your message again!"
95
+ )
96
+
97
+ history.append({"role": "user", "content": message})
98
+ history.append({"role": "assistant", "content": bot_response})
99
+
100
+ return "", history
101
+
102
+ def handle_quick_reply(button_text, history):
103
+ return process_chat(button_text, history)
104
+
105
+ # -------------------------------------------------------------------------
106
+ # 4. Gradio Web Interface Layout
107
+ # -------------------------------------------------------------------------
108
+ # Improved CSS: Explicitly setting foreground text colors to prevent invisible text in dark mode
109
+ custom_css = """
110
+ .header-container { background-color: #f0f4f8 !important; padding: 25px; border-radius: 12px; margin-bottom: 20px; border: 1px solid #e2e8f0; }
111
+ .header-container h1, .header-container p { color: #0f172a !important; }
112
+ .meta-container { text-align: right; font-family: 'Georgia', serif; font-style: italic; }
113
+ .meta-container h3 { color: #334155 !important; margin: 4px 0; }
114
+ .sidebar-panel { background-color: #0f766e !important; color: white !important; padding: 25px; border-radius: 12px; text-align: center; }
115
+ .sidebar-panel p, .sidebar-panel h2 { color: white !important; }
116
+ .quick-btn { background-color: #f1f5f9 !important; border: 1px solid #cbd5e1 !important; color: #0f766e !important; font-weight: 600; }
117
+ .quick-btn:hover { background-color: #e2e8f0 !important; }
118
+ """
119
+
120
+ # Native JS logic to toggle Gradio's standard dark mode class on the body
121
+ toggle_js = """
122
+ function() {
123
+ document.body.classList.toggle('dark');
124
+ }
125
+ """
126
+
127
+ with gr.Blocks(title="Simplilearn MOOC FAQ Agent") as demo:
128
+
129
+ # TOP HEADER SECTION
130
+ with gr.Row(elem_classes=["header-container"]):
131
+ with gr.Column(scale=3):
132
+ gr.Markdown("""
133
+ # About Us
134
+ At Simplilearn Agentic Hub, we are passionate about the power of structured education and its ability
135
+ to transform careers. We are a leading knowledge processing framework dedicated to helping
136
+ learners unlock the full potential of specialized MOOC educational pathways.
137
+ """)
138
+ with gr.Column(scale=2, elem_classes=["meta-container"]):
139
+ current_date = datetime.datetime.now().strftime("%dth %B %Y | Time: %I:%M%p").lower()
140
+ gr.Markdown(f"### {current_date}")
141
+ mode_btn = gr.Button("☀️ 🌙 ➔ Change Mode (Day / Night)", variant="secondary", size="sm")
142
+ mode_btn.click(fn=None, js=toggle_js)
143
+
144
+ # MAIN WORKSPACE SECTION
145
+ with gr.Row():
146
+ # Chat interface
147
+ with gr.Column(scale=4):
148
+ chatbot = gr.Chatbot(
149
+ label="Lane AI Support Stream",
150
+ value=[{"role": "assistant", "content": "Hello! I'm Lane, your Simplilearn Support Agent. How can I assist you with your courses today?"}]
151
+ )
152
+
153
+ with gr.Row():
154
+ user_input = gr.Textbox(
155
+ show_label=False,
156
+ placeholder="Ask Lane anything regarding Simplilearn policies...",
157
+ scale=5
158
+ )
159
+ submit_btn = gr.Button("Send", variant="primary", scale=1)
160
+
161
+ with gr.Row():
162
+ btn_refund = gr.Button("What is the course refund policy?", elem_classes=["quick-btn"])
163
+ btn_cert = gr.Button("How do I unlock my completion certificate?", elem_classes=["quick-btn"])
164
+
165
+ # Agent Sidebar Identity
166
+ with gr.Column(scale=2, elem_classes=["sidebar-panel"]):
167
+ gr.HTML("""
168
+ <div style='margin-top: 10px;'>
169
+ <img src='https://cdn-icons-png.flaticon.com/512/4712/4712010.png' alt='Lane Avatar' style='width:120px; height:120px; object-fit:cover; margin:0 auto 15px auto; border: 4px solid #ffffff; border-radius: 50%; background-color: white;'>
170
+ <h2 style='margin-bottom: 5px; font-size: 24px; font-weight: bold;'>Lane <span style='background-color:#ffffff; color:#0f766e; padding:2px 8px; border-radius:4px; font-size:14px; vertical-align: middle;'>AI</span></h2>
171
+ <p style='font-style: italic; opacity: 0.9; margin-bottom: 15px;'>Your Agentic Learning Assistant</p>
172
+ <hr style='border-color: rgba(255,255,255,0.2); margin: 15px 0;'>
173
+ <p style='font-size: 14px; text-align: left; line-height: 1.5; opacity: 0.95;'>
174
+ 🎓 <b>Secure Session Activated</b><br>
175
+ Operational Node: Simplilearn-Chroma-01<br>
176
+ Processing Hardware: Base CPU Node
177
+ </p>
178
+ </div>
179
+ """)
180
+
181
+ # Event Wiring Architecture
182
+ submit_btn.click(
183
+ fn=process_chat,
184
+ inputs=[user_input, chatbot],
185
+ outputs=[user_input, chatbot]
186
+ )
187
+ user_input.submit(
188
+ fn=process_chat,
189
+ inputs=[user_input, chatbot],
190
+ outputs=[user_input, chatbot]
191
+ )
192
+
193
+ btn_refund.click(
194
+ fn=handle_quick_reply,
195
+ inputs=[btn_refund, chatbot],
196
+ outputs=[user_input, chatbot]
197
+ )
198
+ btn_cert.click(
199
+ fn=handle_quick_reply,
200
+ inputs=[btn_cert, chatbot],
201
+ outputs=[user_input, chatbot]
202
+ )
203
+
204
+ if __name__ == "__main__":
205
+ # Launch with Gradio's Default theme to ensure dark/light mode hooks exist natively
206
+ demo.launch(server_name="0.0.0.0", server_port=7860, css=custom_css, theme=gr.themes.Default())