| |
| import streamlit as st |
| import PyPDF2 |
| import openai |
| import faiss |
| import os |
| import numpy as np |
| from sklearn.feature_extraction.text import TfidfVectorizer |
| from sklearn.metrics.pairwise import cosine_similarity |
| from io import StringIO |
| from PIL import Image |
| import base64 |
| from io import BytesIO |
|
|
| |
| def image_to_base64(image): |
| buffered = BytesIO() |
| image.save(buffered, format="PNG") |
| return base64.b64encode(buffered.getvalue()).decode("utf-8") |
|
|
| |
| def extract_text_from_pdf(pdf_file): |
| reader = PyPDF2.PdfReader(pdf_file) |
| text = "" |
| for page in reader.pages: |
| text += page.extract_text() |
| return text |
|
|
| |
| def get_embeddings(text, model="text-embedding-ada-002"): |
| response = openai.Embedding.create(input=[text], model=model) |
| return response['data'][0]['embedding'] |
|
|
| |
| def search_similar(query_embedding, index, stored_texts, top_k=3): |
| distances, indices = index.search(np.array([query_embedding]), top_k) |
| results = [(stored_texts[i], distances[0][idx]) for idx, i in enumerate(indices[0])] |
| return results |
|
|
| |
| def generate_code_from_prompt(prompt, model="gpt-4o-mini"): |
| response = openai.ChatCompletion.create( |
| model=model, |
| messages=[{"role": "user", "content": prompt}] |
| ) |
| return response['choices'][0]['message']['content'] |
|
|
| |
| def save_code_to_file(code, filename="generated_code.txt"): |
| with open(filename, "w") as f: |
| f.write(code) |
|
|
| |
| def generate_summary(text): |
| prompt = f"Summarize the following text into key points:\n\n{text}" |
| response = openai.ChatCompletion.create( |
| model="gpt-4o-mini", |
| messages=[{"role": "user", "content": prompt}] |
| ) |
| return response['choices'][0]['message']['content'] |
|
|
| |
| def fix_code_bugs(buggy_code, model="gpt-4o-mini"): |
| prompt = f"The following code has bugs or issues. Please identify and fix the problems. If possible, provide explanations for the changes made.\n\nBuggy Code:\n{buggy_code}\n\nFixed Code:" |
| response = openai.ChatCompletion.create( |
| model=model, |
| messages=[{"role": "user", "content": prompt}] |
| ) |
| return response['choices'][0]['message']['content'] |
|
|
| |
| def generate_math_solution(query): |
| prompt = f"Explain and solve the following mathematical problem step by step: {query}" |
| response = openai.ChatCompletion.create( |
| model="gpt-3.5-turbo", |
| messages=[{"role": "user", "content": prompt}] |
| ) |
| return response['choices'][0]['message']['content'] |
|
|
| |
| st.set_page_config(page_title="AI Assistance", page_icon=":robot:", layout="wide") |
|
|
| |
| st.markdown(""" |
| <style> |
| body { |
| background-color: #f0f4f7; |
| font-family: 'Arial', sans-serif; |
| color: #333; |
| } |
| .header { |
| text-align: center; |
| font-size: 2.5em; |
| font-weight: bold; |
| color: #4CAF50; |
| margin-top: 30px; |
| animation: fadeIn 2s ease-out; |
| } |
| .sidebar .sidebar-content { |
| background-color: #333; |
| color: white; |
| } |
| .sidebar .sidebar-content a { |
| color: white; |
| font-size: 1.2em; |
| } |
| .sidebar .sidebar-content a:hover { |
| color: #4CAF50; |
| } |
| .stButton>button { |
| background-color: #4CAF50; |
| color: white; |
| font-size: 1.2em; |
| padding: 10px 20px; |
| border-radius: 5px; |
| border: none; |
| transition: background-color 0.3s; |
| } |
| .stButton>button:hover { |
| background-color: #45a049; |
| } |
| .stTextInput input { |
| padding: 10px; |
| font-size: 1.1em; |
| border-radius: 5px; |
| border: 1px solid #ccc; |
| } |
| .stFileUploader { |
| border-radius: 5px; |
| border: 1px solid #ddd; |
| padding: 10px; |
| } |
| .stImage { |
| animation: fadeIn 2s ease-out; |
| } |
| @keyframes fadeIn { |
| 0% { opacity: 0; } |
| 100% { opacity: 1; } |
| } |
| .stTextArea textarea { |
| padding: 10px; |
| font-size: 1.1em; |
| border-radius: 5px; |
| border: 1px solid #ccc; |
| } |
| </style> |
| """, unsafe_allow_html=True) |
|
|
| |
| st.markdown(""" |
| <script type="text/javascript"> |
| window.onload = function() { |
| const elements = document.querySelectorAll('.stButton button, .stTextInput input, .stTextArea textarea'); |
| elements.forEach(element => { |
| element.style.transition = 'all 0.3s ease'; |
| element.addEventListener('mouseover', function() { |
| element.style.transform = 'scale(1.05)'; |
| }); |
| element.addEventListener('mouseout', function() { |
| element.style.transform = 'scale(1)'; |
| }); |
| }); |
| }; |
| </script> |
| """, unsafe_allow_html=True) |
|
|
| |
| st.markdown("<h1 class='header'>Smart AI Tutor Assistance</h1>", unsafe_allow_html=True) |
|
|
| |
| image = Image.open("14313824.png") |
|
|
| |
| st.markdown( |
| """ |
| <div style="display: flex; justify-content: center;"> |
| <img src="data:image/png;base64,{}" width="200"> |
| </div> |
| """.format(image_to_base64(image)), |
| unsafe_allow_html=True |
| ) |
|
|
| |
| openai_api_key = st.text_input("Enter your OpenAI API key:", type="password") |
|
|
| if openai_api_key: |
| openai.api_key = openai_api_key |
|
|
| |
| st.sidebar.title("Select Mode") |
| mode = st.sidebar.radio("Choose an option", ( |
| "Course Query Assistant", |
| "Code Generator", |
| "AI Chatbot Tutor", |
| "AI Study Notes & Summaries", |
| "Code Bug Fixer", |
| "Mathematics Assistant", |
| "Biology Assistant", |
| "Chemistry Assistant", |
| "Physics Assistant", |
| "Voice Chat", |
| "Image Chat", |
| "English To Japanese", |
| "Text to Image Generator", |
| "Graph Tutorial", |
| "Text-To-Diagram-Generator", |
| "Multi Modal Omni Chatbot", |
| "GPT-4.5 Preview Chatbot" |
| )) |
|
|
| |
| st.sidebar.markdown(""" |
| ## Contact |
| |
| For any questions or issues, please contact: |
| |
| - **Email**: [shukdevdatta@gmail.com](mailto:shukdevdatta@gmail.com) |
| - **GitHub**: [Click here to access the Github Profile](https://github.com/shukdevtroy) |
| - **WhatsApp**: [Click here to chat](https://wa.me/+8801719296601) |
| - **HuggingFace Profile**: [Click here to access the HuggingFace Profile](https://huggingface.co/shukdevdatta123) |
| """) |
|
|
| if mode == "Course Query Assistant": |
| st.header("Course Query Assistant") |
| |
| course_query_image = Image.open("Capture.PNG") |
| st.image(course_query_image, width=150) |
|
|
| |
| uploaded_files = st.file_uploader("Upload Course Materials (PDFs)", type=["pdf"], accept_multiple_files=True) |
|
|
| if uploaded_files: |
| st.write("Processing uploaded course materials...") |
|
|
| |
| course_texts = [] |
| for uploaded_file in uploaded_files: |
| text = extract_text_from_pdf(uploaded_file) |
| course_texts.append(text) |
|
|
| |
| combined_text = " ".join(course_texts) |
|
|
| |
| chunks = [combined_text[i:i+1000] for i in range(0, len(combined_text), 1000)] |
|
|
| |
| embeddings = [get_embeddings(chunk) for chunk in chunks] |
|
|
| |
| embeddings_np = np.array(embeddings).astype("float32") |
|
|
| |
| index = faiss.IndexFlatL2(len(embeddings_np[0])) |
| index.add(embeddings_np) |
|
|
| st.write("Course materials have been processed and indexed.") |
|
|
| |
| query = st.text_input("Enter your question about the course materials:") |
|
|
| if query: |
| |
| query_embedding = get_embeddings(query) |
|
|
| |
| results = search_similar(query_embedding, index, chunks) |
|
|
| |
| context = "\n".join([result[0] for result in results]) |
| modified_prompt = f"Context: {context}\n\nQuestion: {query}\n\nProvide a detailed answer based on the context." |
|
|
| |
| response = openai.ChatCompletion.create( |
| model="gpt-4o-mini", |
| messages=[{"role": "user", "content": modified_prompt}] |
| ) |
|
|
| |
| response_content = response['choices'][0]['message']['content'] |
|
|
| |
| st.write("### Intelligent Reply:") |
| st.write(response_content) |
|
|
| elif mode == "Code Generator": |
| st.header("Code Generator") |
|
|
| |
| codegen = Image.open("9802381.png") |
| st.image(codegen, width=150) |
|
|
| |
| code_prompt = st.text_area("Describe the code you want to generate:", |
| "e.g., Write a Python program that generates Fibonacci numbers.") |
| |
| if st.button("Generate Code"): |
| if code_prompt: |
| with st.spinner("Generating code..."): |
| |
| generated_code = generate_code_from_prompt(code_prompt) |
| |
| |
| clean_code = "\n".join([line for line in generated_code.splitlines() if not line.strip().startswith("#")]) |
|
|
| |
| save_code_to_file(clean_code) |
|
|
| |
| st.write("### Generated Code:") |
| st.code(clean_code, language="python") |
|
|
| |
| with open("generated_code.txt", "w") as f: |
| f.write(clean_code) |
|
|
| st.download_button( |
| label="Download Generated Code", |
| data=open("generated_code.txt", "rb").read(), |
| file_name="generated_code.txt", |
| mime="text/plain" |
| ) |
| else: |
| st.error("Please provide a prompt to generate the code.") |
|
|
| elif mode == "AI Chatbot Tutor": |
| st.header("AI Chatbot Tutor") |
|
|
| |
| aitut = Image.open("910372.png") |
| st.image(aitut, width=150) |
|
|
| |
| chat_history = [] |
|
|
| def chat_with_bot(query): |
| chat_history.append({"role": "user", "content": query}) |
| response = openai.ChatCompletion.create( |
| model="gpt-4o-mini", |
| messages=chat_history |
| ) |
| chat_history.append({"role": "assistant", "content": response['choices'][0]['message']['content']}) |
| return response['choices'][0]['message']['content'] |
|
|
| user_query = st.text_input("Ask a question:") |
|
|
| if user_query: |
| with st.spinner("Getting answer..."): |
| bot_response = chat_with_bot(user_query) |
| st.write(f"### AI Response: {bot_response}") |
|
|
| elif mode == "AI Study Notes & Summaries": |
| st.header("AI Study Notes & Summaries") |
|
|
| |
| aisum = Image.open("sum.png") |
| st.image(aisum, width=150) |
|
|
| |
| uploaded_files_for_summary = st.file_uploader("Upload Course Materials (PDFs) for Summarization", type=["pdf"], accept_multiple_files=True) |
|
|
| if uploaded_files_for_summary: |
| st.write("Generating study notes and summaries...") |
|
|
| |
| all_text = "" |
| for uploaded_file in uploaded_files_for_summary: |
| text = extract_text_from_pdf(uploaded_file) |
| all_text += text |
|
|
| |
| summary = generate_summary(all_text) |
|
|
| |
| st.write("### AI-Generated Summary:") |
| st.write(summary) |
|
|
| elif mode == "Code Bug Fixer": |
| st.header("Code Bug Fixer") |
|
|
| |
| aibug = Image.open("bug.png") |
| st.image(aibug, width=150) |
|
|
| |
| buggy_code = st.text_area("Enter your buggy code here:") |
|
|
| if st.button("Fix Code"): |
| if buggy_code: |
| with st.spinner("Fixing code..."): |
| |
| fixed_code = fix_code_bugs(buggy_code) |
| |
| |
| st.write("### Fixed Code:") |
| st.code(fixed_code, language="python") |
|
|
| |
| with open("fixed_code.txt", "w") as f: |
| f.write(fixed_code) |
|
|
| st.download_button( |
| label="Download Fixed Code", |
| data=open("fixed_code.txt", "rb").read(), |
| file_name="fixed_code.txt", |
| mime="text/plain" |
| ) |
| else: |
| st.error("Please enter some buggy code to fix.") |
|
|
| elif mode == "Mathematics Assistant": |
| st.header("Mathematics Assistant") |
|
|
| |
| math_icon = Image.open("math_icon.PNG") |
| st.image(math_icon, width=150) |
|
|
| |
| math_query = st.text_input("Ask a mathematics-related question:") |
|
|
| if st.button("Solve Problem"): |
| if math_query: |
| with st.spinner("Generating solution..."): |
| |
| solution = generate_math_solution(math_query) |
|
|
| |
| formatted_solution = f""" |
| ### Solution to the Problem |
| **Problem:** {math_query} |
| |
| **Solution:** |
| |
| {solution} |
| """ |
|
|
| st.markdown(formatted_solution) |
| else: |
| st.error("Please enter a math problem to solve.") |
|
|
| |
| elif mode == "Biology Assistant": |
| st.header("Biology Assistant") |
|
|
| |
| bio_icon = Image.open("bio_icon.PNG") |
| st.image(bio_icon, width=150) |
|
|
| |
| bio_query = st.text_input("Ask a biology-related question:") |
|
|
| if bio_query: |
| with st.spinner("Getting answer..."): |
| prompt = f"Answer the following biology question: {bio_query}" |
| response = openai.ChatCompletion.create( |
| model="gpt-4o-mini", |
| messages=[{"role": "user", "content": prompt}] |
| ) |
| answer = response['choices'][0]['message']['content'] |
| st.write(f"### Answer: {answer}") |
|
|
| |
| elif mode == "Chemistry Assistant": |
| st.header("Chemistry Assistant") |
|
|
| |
| chem_icon = Image.open("chem.PNG") |
| st.image(chem_icon, width=150) |
|
|
| |
| chem_query = st.text_input("Ask a chemistry-related question:") |
|
|
| if chem_query: |
| with st.spinner("Getting answer..."): |
| prompt = f"Answer the following chemistry question: {chem_query}" |
| response = openai.ChatCompletion.create( |
| model="gpt-4o-mini", |
| messages=[{"role": "user", "content": prompt}] |
| ) |
| answer = response['choices'][0]['message']['content'] |
| st.write(f"### Answer: {answer}") |
|
|
| |
| elif mode == "Physics Assistant": |
| st.header("Physics Assistant") |
|
|
| |
| phys_icon = Image.open("physics_icon.PNG") |
| st.image(phys_icon, width=150) |
|
|
| |
| phys_query = st.text_input("Ask a physics-related question:") |
|
|
| if phys_query: |
| with st.spinner("Getting answer..."): |
| prompt = f"Answer the following physics question: {phys_query}" |
| response = openai.ChatCompletion.create( |
| model="gpt-3.5-turbo", |
| messages=[{"role": "user", "content": prompt}] |
| ) |
| answer = response['choices'][0]['message']['content'] |
| st.write(f"### Answer: {answer}") |
|
|
| |
| elif mode == "Voice Chat": |
| st.header("Voice Chat") |
|
|
| |
| st.write("Click the button below to go to the Voice Chat.") |
|
|
| |
| gif = "200w.gif" |
| st.image(gif, use_container_width=50) |
|
|
| |
| if st.button("Go to Voice Chat"): |
| st.write("Redirecting to the voice chat...") |
| st.markdown(f'<a href="https://shukdevdatta123-voicechat.hf.space" target="_blank">Go to Voice Chat</a>', unsafe_allow_html=True) |
|
|
| |
| elif mode == "Image Chat": |
| |
| st.header("Image Chat") |
|
|
| |
| imgc = Image.open("i.jpg") |
| st.image(imgc, width=150) |
|
|
| |
| st.write("Click the button below to go to the Image Chat.") |
|
|
| |
| gif = "200w.gif" |
| st.image(gif, use_container_width=50) |
|
|
| |
| if st.button("Go to Image Chat"): |
| st.write("Redirecting to the image chat...") |
| st.markdown(f'<a href="https://imagechat2278.streamlit.app/" target="_blank">Go to Image Chat</a>', unsafe_allow_html=True) |
|
|
| |
| if st.button("Go to Image Chat (Alternative App)"): |
| st.write("Redirecting to the alternative image chat...") |
| st.markdown(f'<a href="https://imagechat.onrender.com/" target="_blank">Go to Image Chat (Alternative App)</a>', unsafe_allow_html=True) |
|
|
| |
| elif mode == "English To Japanese": |
| st.header("English To Japanese") |
|
|
| |
| st.write("Click the button below to go to the English To Japanese Translator.") |
|
|
|
|
| gif = "200w.gif" |
| st.image(gif, use_container_width=150) |
|
|
| |
| if st.button("Go to English To Japanese Translator"): |
| st.write("Redirecting to the English To Japanese Translator...") |
| st.markdown(f'<a href="https://shukdevdatta123-engtojap-2-0.hf.space" target="_blank">Go to English To Japanese Translator</a>', unsafe_allow_html=True) |
|
|
| |
| elif mode == "Text to Image Generator": |
| st.header("Text to Image Generator") |
|
|
| |
| st.write("Click the button below to go to the Text to Image Generator.") |
|
|
|
|
| gif = "200w.gif" |
| st.image(gif, use_container_width=150) |
|
|
| |
| if st.button("Go to Text to Image Generator"): |
| st.write("Redirecting to the Text to Image Generator...") |
| st.markdown(f'<a href="https://shukdevdatta123-image-generator-dall-e3.hf.space" target="_blank">Go to Text to Image Generator</a>', unsafe_allow_html=True) |
| |
| |
| elif mode == "Graph Tutorial": |
| st.header("Graph Tutorial") |
|
|
| |
| st.write("Click the button below to go to Graph Tutorial.") |
|
|
|
|
| gif = "200w.gif" |
| st.image(gif, use_container_width=150) |
|
|
| |
| if st.button("Go to Graph Tutorial"): |
| st.write("Redirecting to Graph Tutorial...") |
| st.markdown(f'<a href="https://shukdevdatta123-networkx-tutorial.hf.space" target="_blank">Go to Graph Tutorial</a>', unsafe_allow_html=True) |
|
|
| |
| elif mode == "Text-To-Diagram-Generator": |
| st.header("Text-To-Diagram-Generator") |
|
|
| |
| st.write("Click the button below to go to Text-To-Diagram-Generator.") |
|
|
|
|
| gif = "200w.gif" |
| st.image(gif, use_container_width=150) |
|
|
| |
| if st.button("Go to Text-To-Diagram-Generator"): |
| st.write("Redirecting to Text-To-Diagram-Generator...") |
| st.markdown(f'<a href="https://shukdevdatta123-text-2-diagram.hf.space" target="_blank">Go to Text-To-Diagram-Generator</a>', unsafe_allow_html=True) |
|
|
| |
| elif mode == "Multi Modal Omni Chatbot": |
| st.header("Multi Modal Omni Chatbot") |
|
|
| |
| st.write("Click the button below to go to Multi Modal Omni Chatbot.") |
|
|
|
|
| gif = "200w.gif" |
| st.image(gif, use_container_width=150) |
|
|
| |
| if st.button("Go to Multi Modal Omni Chatbot"): |
| st.write("Redirecting to Multi Modal Omni Chatbot...") |
| st.markdown(f'<a href="https://shukdevdatta123-multi-modal-o1-chatbot.hf.space" target="_blank">Go to Multi Modal Omni Chatbot</a>', unsafe_allow_html=True) |
|
|
| |
| elif mode == "GPT-4.5 Preview Chatbot": |
| st.header("GPT-4.5 Preview Chatbot") |
|
|
| |
| st.write("Click the button below to go to Multi Modal Omni Chatbot.") |
|
|
|
|
| gif = "200w.gif" |
| st.image(gif, use_container_width=150) |
|
|
| |
| if st.button("Go to GPT-4.5 Preview Chatbot"): |
| st.write("Redirecting to GPT-4.5 Preview Chatbot...") |
| st.markdown(f'<a href="https://shukdevdatta123-gpt-4-5-multimodal-chatbot.hf.space" target="_blank">Go to GPT-4.5 Preview Chatbot</a>', unsafe_allow_html=True) |