Chia Woon Yap commited on
Commit
a712245
·
verified ·
1 Parent(s): 9f948da

Delete app_R2.py

Browse files
Files changed (1) hide show
  1. app_R2.py +0 -448
app_R2.py DELETED
@@ -1,448 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- """app
3
-
4
- Automatically generated by Colab.
5
-
6
- Original file is located at
7
- https://colab.research.google.com/drive/1pwwcBb5Zlw1DA3u5K8W8mjrwBTBWXc1L
8
- """
9
-
10
- import gradio as gr
11
- import numpy as np
12
- from transformers import pipeline
13
- import os
14
- import time
15
- import groq
16
- import uuid # For generating unique filenames
17
-
18
- # Updated imports to address LangChain deprecation warnings:
19
- from langchain_groq import ChatGroq
20
- from langchain.schema import HumanMessage
21
- from langchain.text_splitter import RecursiveCharacterTextSplitter
22
- from langchain_community.vectorstores import Chroma
23
- from langchain_community.embeddings import HuggingFaceEmbeddings
24
- from langchain.docstore.document import Document
25
-
26
- # Importing chardet (make sure to add chardet to your requirements.txt)
27
- import chardet
28
-
29
- import fitz # PyMuPDF for PDFs
30
- import docx # python-docx for Word files
31
- import gtts # Google Text-to-Speech library
32
- from pptx import Presentation # python-pptx for PowerPoint files
33
- import re
34
-
35
- # Initialize Whisper model for speech-to-text
36
- transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en")
37
-
38
- # Set API Key (Ensure it's stored securely in an environment variable)
39
- groq.api_key = os.getenv("GROQ_API_KEY") # Replace with a valid API key
40
-
41
- #___________________________________
42
-
43
- # Authenticate with Hugging Face API using the token
44
- #hf_token = os.getenv("HF_TOKEN") # Replace with the environment variable containing your Hugging Face token
45
- #login(token=hf_token)
46
-
47
- # Load the LLaVA model
48
- #model_id = "liuhaotian/LLaVA-7B" # You can change the model ID based on what is available
49
- #processor = AutoProcessor.from_pretrained(model_id)
50
- #model = AutoModelForVision2Seq.from_pretrained(model_id, torch_dtype=torch.float16).cuda()
51
-
52
- # Load and preprocess an image
53
- #image = Image.open("your_image.jpg") # Replace with the path to your image
54
- #inputs = processor(text="Describe this image", images=image, return_tensors="pt").to("cuda")
55
-
56
- # Generate output from LLaVA model
57
- #output = model.generate(**inputs)
58
-
59
- # Decode and print the output
60
- #print(processor.decode(output[0]))
61
-
62
- #___________________________________
63
-
64
- # Initialize Chat Model
65
- chat_model = ChatGroq(model_name="llama-3.3-70b-versatile", api_key=groq.api_key) #DeepSeek-R1-Distill-Llama-70b , llama-3.3-70b-versatile , deepseek-r1-distill-qwen-32b
66
-
67
- # Initialize Embeddings and chromaDB
68
-
69
- embedding_model = HuggingFaceEmbeddings()
70
- vectorstore = Chroma(embedding_function=embedding_model)
71
-
72
-
73
- # -*- coding: utf-8 -*-
74
- """app
75
-
76
- Automatically generated by Colab.
77
-
78
- Original file is located at
79
- https://colab.research.google.com/drive/1jdKA4WQJcLb0_aQ3vtGVM46B1wriSsDv
80
- """
81
-
82
- import gradio as gr
83
- import numpy as np
84
- from transformers import pipeline
85
- import os
86
- import time
87
- import groq
88
- import uuid # For generating unique filenames
89
-
90
- # Updated imports to address LangChain deprecation warnings:
91
- from langchain_groq import ChatGroq
92
- from langchain.schema import HumanMessage
93
- from langchain.text_splitter import RecursiveCharacterTextSplitter
94
- from langchain_community.vectorstores import Chroma
95
- from langchain_community.embeddings import HuggingFaceEmbeddings
96
- from langchain.docstore.document import Document
97
-
98
- # Importing chardet (make sure to add chardet to your requirements.txt)
99
- import chardet
100
-
101
- import fitz # PyMuPDF for PDFs
102
- import docx # python-docx for Word files
103
- import gtts # Google Text-to-Speech library
104
- from pptx import Presentation # python-pptx for PowerPoint files
105
- import re
106
-
107
-
108
- # Initialize Whisper model for speech-to-text
109
- transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en")
110
-
111
- # Set API Key (Ensure it's stored securely in an environment variable)
112
- groq.api_key = os.getenv("GROQ_API_KEY")
113
-
114
- # Initialize Chat Model
115
- chat_model = ChatGroq(model_name="llama-3.3-70b-versatile", api_key=groq.api_key) #DeepSeek-R1-Distill-Llama-70b | deepseek-r1-distill-qwen-32b
116
-
117
- # Initialize Embeddings and chromaDB
118
- os.makedirs("chroma_db", exist_ok=True)
119
- embedding_model = HuggingFaceEmbeddings()
120
- #new
121
- vectorstore = Chroma(
122
- embedding_function=embedding_model,
123
- persist_directory="chroma_db" # Set a valid folder name or path
124
- )
125
- vectorstore.persist()
126
- #end New
127
-
128
- # Short-term memory for the LLM
129
- chat_memory = []
130
-
131
- # Prompt for quiz generation with added remark
132
- quiz_prompt = """
133
- You are an AI assistant specialized in education and assessment creation. Given an uploaded document or text, generate a quiz with a mix of multiple-choice questions (MCQs) and fill-in-the-blank questions. The quiz should be directly based on the key concepts, facts, and details from the provided material.
134
- Generate 20 Questions.
135
- Remove all unnecessary formatting generated by the LLM, including <think> tags, asterisks, markdown formatting, and any bold or italic text, as well as **, ###, ##, and # tags.
136
- For each question:
137
- - Provide 4 answer choices (for MCQs), with only one correct answer.
138
- - Ensure fill-in-the-blank questions focus on key terms, phrases, or concepts from the document.
139
- - Include an answer key for all questions.
140
- - Ensure questions vary in difficulty and encourage comprehension rather than memorization.
141
- - Additionally, implement an instant feedback mechanism:
142
- - When a user selects an answer, indicate whether it is correct or incorrect.
143
- - If incorrect, provide a brief explanation from the document to guide learning.
144
- - Ensure responses are concise and educational to enhance understanding.
145
- Output Example:
146
- 1. Fill in the blank: The LLM Agent framework has a central decision-making unit called the _______________________.
147
-
148
- Answer: Agent Core
149
-
150
- Feedback: The Agent Core is the central component of the LLM Agent framework, responsible for managing goals, tool instructions, planning modules, memory integration, and agent persona.
151
-
152
- 2. What is the main limitation of LLM-based applications?
153
- a) Limited token capacity
154
- b) Lack of domain expertise
155
- c) Prone to hallucination
156
- d) All of the above
157
-
158
- Answer: d) All of the above
159
-
160
- Feedback: LLM-based applications have several limitations, including limited token capacity, lack of domain expertise, and being prone to hallucination, among others.
161
-
162
- 3. Given the following info, what is the value of P(jam|Rain)?
163
- P(no Rain) = 0.8;
164
- P(no Jam) = 0.2;
165
- P(Rain|Jam) = 0.1
166
-
167
- a) 0.016
168
- b) 0.025
169
- c) 0.1
170
- d) 0.4
171
-
172
- Answer: d) 0.4
173
-
174
- Feedback: This question tests understanding of Bayes’ Theorem by requiring the calculation of conditional probability using the given values.
175
- """
176
-
177
- # Function to clean AI response by removing unwanted formatting
178
- def clean_response(response):
179
- """Removes <think> tags, asterisks, and markdown formatting."""
180
- cleaned_text = re.sub(r"<think>.*?</think>", "", response, flags=re.DOTALL)
181
- cleaned_text = re.sub(r"(\*\*|\*|\[|\])", "", cleaned_text)
182
- cleaned_text = re.sub(r"^##+\s*", "", cleaned_text, flags=re.MULTILINE)
183
- cleaned_text = re.sub(r"\\", "", cleaned_text)
184
- cleaned_text = re.sub(r"---", "", cleaned_text)
185
- return cleaned_text.strip()
186
-
187
- # Function to generate quiz based on content
188
- def generate_quiz(content):
189
- prompt = f"{quiz_prompt}\n\nDocument content:\n{content}"
190
- response = chat_model([HumanMessage(content=prompt)])
191
- cleaned_response = clean_response(response.content)
192
- return cleaned_response
193
-
194
- # Function to retrieve relevant documents from vectorstore based on user query
195
- def retrieve_documents(query):
196
- results = vectorstore.similarity_search(query, k=3)
197
- return [doc.page_content for doc in results]
198
-
199
- # Function to handle chatbot interactions with short-term memory
200
- def chat_with_groq(user_input):
201
- try:
202
- # Retrieve relevant documents for additional context
203
- relevant_docs = retrieve_documents(user_input)
204
- context = "\n".join(relevant_docs) if relevant_docs else "No relevant documents found."
205
-
206
- # Construct proper prompting with conversation history
207
- system_prompt = "You are a helpful AI assistant. Answer questions accurately and concisely."
208
- conversation_history = "\n".join(chat_memory[-10:]) # Keep the last 10 exchanges
209
- prompt = f"{system_prompt}\n\nConversation History:\n{conversation_history}\n\nUser Input: {user_input}\n\nContext:\n{context}"
210
-
211
- # Call the chat model
212
- response = chat_model([HumanMessage(content=prompt)])
213
-
214
- # Clean response to remove any unwanted formatting
215
- cleaned_response_text = clean_response(response.content)
216
-
217
- # Append conversation history
218
- chat_memory.append(f"User: {user_input}")
219
- chat_memory.append(f"AI: {cleaned_response_text}")
220
-
221
- # Convert response to speech
222
- audio_file = speech_playback(cleaned_response_text)
223
-
224
- # Ensure the return format is a list of tuples
225
- return [(user_input, cleaned_response_text)], audio_file
226
- except Exception as e:
227
- return [("Error", str(e))], None
228
-
229
-
230
- # Function to play response as speech using gTTS
231
- def speech_playback(text):
232
- try:
233
- # Generate a unique filename for each audio file
234
- unique_id = str(uuid.uuid4())
235
- audio_file = f"output_audio_{unique_id}.mp3"
236
-
237
- # Convert text to speech
238
- tts = gtts.gTTS(text, lang='en')
239
- tts.save(audio_file)
240
-
241
- # Return the path to the audio file
242
- return audio_file
243
- except Exception as e:
244
- print(f"Error in speech_playback: {e}")
245
- return None
246
-
247
- # Function to detect encoding safely
248
- def detect_encoding(file_path):
249
- try:
250
- with open(file_path, "rb") as f:
251
- raw_data = f.read(4096)
252
- detected = chardet.detect(raw_data)
253
- encoding = detected["encoding"]
254
- return encoding if encoding else "utf-8"
255
- except Exception:
256
- return "utf-8"
257
-
258
- # Function to extract text from PDF
259
- def extract_text_from_pdf(pdf_path):
260
- try:
261
- doc = fitz.open(pdf_path)
262
- text = "\n".join([page.get_text("text") for page in doc])
263
- return text if text.strip() else "No extractable text found."
264
- except Exception as e:
265
- return f"Error extracting text from PDF: {str(e)}"
266
-
267
- # Function to extract text from Word files (.docx)
268
- def extract_text_from_docx(docx_path):
269
- try:
270
- doc = docx.Document(docx_path)
271
- text = "\n".join([para.text for para in doc.paragraphs])
272
- return text if text.strip() else "No extractable text found."
273
- except Exception as e:
274
- return f"Error extracting text from Word document: {str(e)}"
275
-
276
- # Function to extract text from PowerPoint files (.pptx)
277
- def extract_text_from_pptx(pptx_path):
278
- try:
279
- presentation = Presentation(pptx_path)
280
- text = ""
281
- for slide in presentation.slides:
282
- for shape in slide.shapes:
283
- if hasattr(shape, "text"):
284
- text += shape.text + "\n"
285
- return text if text.strip() else "No extractable text found."
286
- except Exception as e:
287
- return f"Error extracting text from PowerPoint: {str(e)}"
288
-
289
- # Function to process documents safely
290
- def process_document(file):
291
- try:
292
- file_extension = os.path.splitext(file.name)[-1].lower()
293
- if file_extension in [".png", ".jpg", ".jpeg"]:
294
- return "Error: Images cannot be processed for text extraction."
295
- if file_extension == ".pdf":
296
- content = extract_text_from_pdf(file.name)
297
- elif file_extension == ".docx":
298
- content = extract_text_from_docx(file.name)
299
- elif file_extension == ".pptx":
300
- content = extract_text_from_pptx(file.name)
301
- else:
302
- encoding = detect_encoding(file.name)
303
- with open(file.name, "r", encoding=encoding, errors="replace") as f:
304
- content = f.read()
305
- text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
306
- documents = [Document(page_content=chunk) for chunk in text_splitter.split_text(content)]
307
- vectorstore.add_documents(documents)
308
- vectorstore.persist() # <-- Persist changes after adding documents
309
-
310
- quiz = generate_quiz(content)
311
- return f"Document processed successfully (File Type: {file_extension}). Quiz generated:\n{quiz}"
312
- except Exception as e:
313
- return f"Error processing document: {str(e)}"
314
-
315
- # Function to handle speech-to-text conversion
316
- def transcribe_audio(audio):
317
- sr, y = audio
318
- if y.ndim > 1:
319
- y = y.mean(axis=1)
320
- y = y.astype(np.float32)
321
- y /= np.max(np.abs(y))
322
- return transcriber({"sampling_rate": sr, "raw": y})["text"]
323
-
324
- # Modify chat_with_groq function to return audio file for playback
325
- def chat_with_groq(user_input):
326
- try:
327
- # Retrieve relevant documents for additional context
328
- relevant_docs = retrieve_documents(user_input)
329
- context = "\n".join(relevant_docs) if relevant_docs else "No relevant documents found."
330
-
331
- # Construct proper prompting with conversation history
332
- system_prompt = "You are a helpful AI assistant. Answer questions accurately and concisely."
333
- conversation_history = "\n".join(chat_memory[-10:]) # Keep the last 10 exchanges
334
- prompt = f"{system_prompt}\n\nConversation History:\n{conversation_history}\n\nUser Input: {user_input}\n\nContext:\n{context}"
335
-
336
- # Call the chat model
337
- response = chat_model([HumanMessage(content=prompt)])
338
-
339
- # Clean response to remove any unwanted formatting
340
- cleaned_response_text = clean_response(response.content)
341
-
342
- # Append conversation history
343
- chat_memory.append(f"User: {user_input}")
344
- chat_memory.append(f"AI: {cleaned_response_text}")
345
-
346
- # Convert response to speech
347
- audio_file = speech_playback(cleaned_response_text)
348
-
349
- # Return both chat response and audio file path
350
- return [(user_input, cleaned_response_text)], audio_file # Return as a tuple
351
- except Exception as e:
352
- return [("Error", str(e))], None
353
-
354
- #__________________________________________________________________________________________________________________________
355
-
356
- def tutor_ai_chatbot():
357
- """Main Gradio interface for the Tutor AI Chatbot."""
358
- with gr.Blocks() as app:
359
- gr.Markdown("# 📚 AI Tutor - We.(POC)")
360
- gr.Markdown("An interactive Personal AI Tutor chatbot to help with your learning needs.")
361
-
362
- # Chatbot Tab
363
- with gr.Tab("AI Chatbot"):
364
- with gr.Row():
365
- with gr.Column(scale=3):
366
- chatbot = gr.Chatbot(height=500) # Chatbot display area
367
- #chatbot = gr.Chatbot(height=500, type="messages")
368
- with gr.Row():
369
- msg = gr.Textbox(label="Ask a question", placeholder="Type your question here...")
370
- submit = gr.Button("Send")
371
-
372
- #with gr.Row():
373
- with gr.Column(scale=1):
374
- audio_input = gr.Audio(type="numpy", label="Record or Upload Audio") # Audio input for speech-to-text
375
-
376
-
377
- with gr.Column(scale=1):
378
- audio_playback = gr.Audio(label="Audio Response", type="filepath")
379
-
380
- # Clear chat history button
381
- clear_btn = gr.Button("Clear Chat")
382
-
383
- # Handle chat interaction
384
- submit.click(
385
- chat_with_groq,
386
- inputs=[msg],
387
- outputs=[chatbot, audio_playback]
388
- )
389
-
390
- # Clear chat history function
391
- def clear_chat_history():
392
- return None, None
393
-
394
- clear_btn.click(clear_chat_history, inputs=None, outputs=[chatbot, audio_playback]) #,audio_input
395
-
396
- # Also allow Enter key to submit
397
- msg.submit(
398
- chat_with_groq,
399
- inputs=[msg],
400
- outputs=[chatbot, audio_playback]
401
- )
402
-
403
- # Add some examples of questions students might ask
404
- with gr.Accordion("Example Questions", open=False):
405
- gr.Examples(
406
- examples=[
407
- "Can you explain the concept of RLHF AI?",
408
- "What are AI transformers?",
409
- "What is MoE AI?",
410
- "What's gate networks AI?",
411
- "I am making a switch, please generating baking recipe?"
412
- ],
413
- inputs=msg
414
- )
415
-
416
- # Upload Notes & Generate Quiz Tab
417
- with gr.Tab("Upload Notes & Generate Quiz"):
418
- with gr.Row():
419
- with gr.Column(scale=2):
420
- file_input = gr.File(label="Upload Lecture Notes (PDF, DOCX, PPTX)")
421
- #generate_btn = gr.Button("Generate Quiz")
422
- with gr.Column(scale=3):
423
- quiz_output = gr.Textbox(label="Generated Quiz", lines=10)
424
-
425
-
426
- # Introduction Video
427
- with gr.Tab("Introduction Video"):
428
- with gr.Row():
429
- with gr.Column(scale=1):
430
- #with gr.Column(scale=1): # Adjust scale for equal width
431
- gr.Markdown("### Welcome to the Introduction Video") # Adding a heading
432
- gr.Markdown("Music from Xu Mengyuan - China-O, musician Xu Mengyuan YUAN! | 徐梦圆 - China-O 音乐人徐梦圆YUAN! ") # Adding descriptive text
433
- #gr.Video("https://github.com/lesterchia1/AI_tutor/raw/main/We%20not%20me%20video.mp4", label="Introduction Video")
434
- #gr.Video("https://huggingface.co/spaces/Lesterchia174/FPOC2_AI-Tutor_Chatbot/raw/main/We%20not%20me%20video.mp4", label="Introduction Video")
435
- gr.Video("we_not_me_video.mp4", label="Introduction Video")
436
-
437
- # Connect the button to the document processing function
438
- audio_input.change(fn=transcribe_audio, inputs=audio_input, outputs=msg) # transcribe and fill the msg textbox
439
- file_input.change(process_document, inputs=file_input, outputs=quiz_output)
440
-
441
-
442
- # Launch the application
443
- app.launch(share=True) # Set share=True to create a public link
444
-
445
-
446
- # Launch the AI chatbot
447
- if __name__ == "__main__":
448
- tutor_ai_chatbot()