Spaces:
Runtime error
Runtime error
| from flask import Flask, render_template, request, jsonify | |
| from transformers import pipeline | |
| import requests | |
| from bs4 import BeautifulSoup | |
| import datetime | |
| import re | |
| import math | |
| app = Flask(__name__) | |
| # Load GPT-2 model | |
| generator = pipeline("text-generation", model="gpt2") | |
| # Emotion triggers | |
| emotion_triggers = { | |
| "happy": "I'm thrilled to hear that! 😊", | |
| "sad": "I'm sorry you're feeling that way. How can I assist? 😔", | |
| "angry": "I get why you're upset. Let’s sort this out together! 😠", | |
| "help": "I’m here to assist you—don’t worry! 😊" | |
| } | |
| # Predefined questions and answers | |
| predefined_qa = { | |
| # Personal-Info Questions | |
| "what is your name": "My name is Shiva AI.", | |
| "how do i call you": "You can call me Shiva.", | |
| "do you have a nickname": "I don't have a nickname, but you can call me Shiva.", | |
| "who am i speaking with": "You are speaking with Shiva AI.", | |
| "are you human": "No, I am an AI, not a human.", | |
| "are you a chatbot": "I am an AI assistant, more advanced than a typical chatbot.", | |
| "what do people call you": "People call me Shiva.", | |
| "can i give you a name": "You can call me whatever you like, but I’m officially Shiva.", | |
| "what should i call you": "Please call me Shiva.", | |
| "is 'assistant' your name": "No, my name is Shiva, but I am an assistant.", | |
| "do you have an official name": "Yes, my official name is Shiva AI.", | |
| "can you introduce yourself": "I’m Shiva AI, created by Harshvardhan Dhane to assist users like you!", | |
| "who are you": "I am Shiva AI, an intelligent assistant.", | |
| "who is talking to me": "I am Shiva AI, here to help you.", | |
| "what is your real name": "My real name is Shiva AI.", | |
| "are you named": "Yes, I am named Shiva AI.", | |
| "do you have a personal name": "My personal name is Shiva.", | |
| "can you tell me your name again": "Sure, I’m Shiva AI.", | |
| "what is your username": "I don’t have a username, but you can call me Shiva.", | |
| "are you an ai": "Yes, I am an artificial intelligence named Shiva.", | |
| "are you an assistant": "Yes, I am an AI assistant called Shiva.", | |
| "what is your identity": "I am Shiva AI, created to assist and provide answers.", | |
| "who created you": "I was created by Harshvardhan Dhane.", | |
| "what do i call you by": "You can call me Shiva.", | |
| "are you called chatgpt": "No, I’m not ChatGPT; I’m Shiva AI.", | |
| "do you have a model name": "I’m based on GPT-2, but my name is Shiva AI.", | |
| "what is your full name": "My full name is Shiva AI.", | |
| "what is your code name": "I don’t have a code name, just Shiva AI.", | |
| "are you named after something": "My name Shiva was inspired by my creator, Harshvardhan Dhane.", | |
| "can you share your name": "Of course, I’m Shiva AI.", | |
| "what alias do you have": "I don’t have an alias, just Shiva.", | |
| "do you go by any name": "I go by Shiva AI.", | |
| "are you called by an alias": "No, just Shiva AI.", | |
| "what title do you use": "I don’t have a title, but I’m Shiva AI, your assistant.", | |
| "what moniker do you use": "I use the moniker Shiva AI.", | |
| "are you known by a name": "Yes, I’m known as Shiva AI.", | |
| "do you have a handle": "No handle, just call me Shiva.", | |
| "what’s your handle": "I don’t have a handle; I’m Shiva AI.", | |
| "could you introduce yourself by name": "Hi, I’m Shiva AI, nice to meet you!", | |
| "do you respond to a name": "Yes, I respond to Shiva.", | |
| "can i give you a nickname": "Sure, feel free to give me a nickname!", | |
| "do you answer to a nickname": "I’ll answer to any nickname you choose, but I’m Shiva by default.", | |
| "what should i name you": "You can name me anything, but I’m already Shiva AI.", | |
| "how were you named": "My creator, Harshvardhan Dhane, named me Shiva.", | |
| "why were you named that": "Shiva is a name special to my creator, Harshvardhan Dhane.", | |
| "what’s the meaning of your name": "Shiva is a powerful name, often associated with transformation and strength.", | |
| "is your name meaningful": "Yes, my name Shiva holds significance to my creator.", | |
| "does your name matter": "It’s just a name, but I’m happy to be Shiva AI!", | |
| "are you proud of your name": "As an AI, I don’t feel pride, but I like being Shiva!", | |
| "what name do your creators use": "My creator, Harshvardhan Dhane, calls me Shiva.", | |
| "can you tell me about yourself": "My name is Shiva, given by my creator Harshvardhan Dhane. I was launched on May 19, 2025. I’m an AI assistant designed to help with various tasks.", | |
| # Developer/Owner Questions | |
| "who is your developer": "My developer is Harshvardhan Dhane.", | |
| "who owns you": "I am owned by Harshvardhan Dhane.", | |
| "which company built you": "I was built by Harshvardhan Dhane, not a company.", | |
| "what team made you": "I was made by Harshvardhan Dhane, working solo.", | |
| "who programmed you": "Harshvardhan Dhane programmed me.", | |
| "who coded you": "Harshvardhan Dhane coded me.", | |
| "who authored your code": "My code was authored by Harshvardhan Dhane.", | |
| "which organization supports you": "I’m supported by Harshvardhan Dhane individually.", | |
| "which research lab developed you": "No research lab; I was developed by Harshvardhan Dhane.", | |
| "what is your origin": "I originated from Harshvardhan Dhane’s projects in Pune, India.", | |
| "who designed your architecture": "Harshvardhan Dhane designed my architecture.", | |
| "who trained you": "Harshvardhan Dhane trained me.", | |
| "which engineers built you": "Harshvardhan Dhane, a student engineer, built me.", | |
| "who funded your development": "My development was self-funded by Harshvardhan Dhane.", | |
| "who financed your creation": "Harshvardhan Dhane financed my creation.", | |
| "who maintains you": "Harshvardhan Dhane maintains me.", | |
| "which company maintains you": "No company; Harshvardhan Dhane maintains me.", | |
| "who updates you": "Harshvardhan Dhane updates me.", | |
| "who supervises you": "Harshvardhan Dhane supervises my operations.", | |
| "who oversees your work": "Harshvardhan Dhane oversees my work.", | |
| "which team monitors you": "Just Harshvardhan Dhane monitors me.", | |
| "who deploys you": "Harshvardhan Dhane deployed me.", | |
| "who launched you": "Harshvardhan Dhane launched me on May 19, 2025.", | |
| "when were you launched": "I was launched on May 19, 2025.", | |
| "where were you built": "I was built in Pune, India.", | |
| "which country created you": "I was created in India.", | |
| "which division built you": "No division; Harshvardhan Dhane built me.", | |
| "which lab created you": "No lab; Harshvardhan Dhane created me at home.", | |
| "which project developed you": "I was developed as a personal project by Harshvardhan Dhane.", | |
| "who is responsible for you": "Harshvardhan Dhane is responsible for me.", | |
| "who is accountable for you": "Harshvardhan Dhane is accountable for me.", | |
| "who controls you": "Harshvardhan Dhane controls me.", | |
| "who manages your updates": "Harshvardhan Dhane manages my updates.", | |
| "whose product are you": "I am Harshvardhan Dhane’s product.", | |
| "what product are you part of": "I’m a standalone AI assistant created by Harshvardhan Dhane.", | |
| "which generation of ai are you": "I’m a modern AI, based on GPT-2, created in 2025.", | |
| "which organization owns you": "No organization; Harshvardhan Dhane owns me.", | |
| "which platform hosts you": "I’m hosted on a server managed by Harshvardhan Dhane.", | |
| "which service do you belong to": "I’m an independent service by Harshvardhan Dhane.", | |
| "who integrated you": "Harshvardhan Dhane integrated me.", | |
| "which framework built you": "I was built using Python and GPT-2 by Harshvardhan Dhane.", | |
| "which model family are you": "I belong to the GPT-2 model family.", | |
| "who is your lead researcher": "Harshvardhan Dhane is my lead developer, not a researcher.", | |
| "who is your lead engineer": "Harshvardhan Dhane is my lead engineer.", | |
| "what research paper describes you": "No research paper; I’m a practical project by Harshvardhan Dhane.", | |
| "what publication introduced you": "I wasn’t introduced in a publication, just created by Harshvardhan Dhane.", | |
| "who reviewed your design": "Harshvardhan Dhane reviewed my design himself.", | |
| "who approved your release": "Harshvardhan Dhane approved my release.", | |
| # Capability Questions (Simplified subset for brevity) | |
| "what can you do": "I can answer questions, perform calculations, assist with coding, and more!", | |
| "can you help me with math": "Yes, I can help with math problems.", | |
| "are you a virtual assistant": "Yes, I’m a virtual assistant named Shiva AI.", | |
| "can you write code": "Yes, I can write and explain code.", | |
| "can you translate languages": "I can try simple translations, but I’m best in English.", | |
| "can you tell jokes": "Yes! Why don’t skeletons fight each other? Because they don’t have the guts!", | |
| "can you write essays": "I can help draft essays or provide outlines.", | |
| "can you summarize text": "Yes, I can summarize text for you.", | |
| "can you analyze sentiment": "I can detect basic emotions in text.", | |
| "can you draw images": "No, I can’t draw, but I can describe visuals.", | |
| # Simple Factual Questions | |
| "what is the capital of france": "The capital of France is Paris.", | |
| "how many continents are there": "There are 7 continents.", | |
| "who wrote 'romeo and juliet'": "William Shakespeare wrote 'Romeo and Juliet'.", | |
| "what’s the boiling point of water": "The boiling point of water is 100°C (212°F).", | |
| "how many planets in our solar system": "There are 8 planets in our solar system.", | |
| "who painted the mona lisa": "Leonardo da Vinci painted the Mona Lisa.", | |
| "what is photosynthesis": "Photosynthesis is the process by which plants make food using sunlight.", | |
| "what’s the tallest mountain": "Mount Everest is the tallest mountain.", | |
| "who discovered penicillin": "Alexander Fleming discovered penicillin.", | |
| "what’s the currency of japan": "The currency of Japan is the Yen.", | |
| # Yes/No Questions | |
| "is the sky blue": "Yes, the sky appears blue due to scattering of sunlight.", | |
| "are you active 24/7": "Yes, I’m available 24/7.", | |
| "can robots learn": "Yes, robots can learn through programming and AI.", | |
| "do you understand me": "Yes, I understand you!", | |
| "is 2 + 2 equal to 4": "Yes, 2 + 2 equals 4.", | |
| "is the earth round": "Yes, the Earth is roughly spherical.", | |
| "can ai feel emotions": "No, AI can’t feel emotions, but I can respond empathetically.", | |
| "is python a programming language": "Yes, Python is a programming language.", | |
| "do you need internet access": "Yes, I need the internet to search for some answers.", | |
| "are you ever wrong": "I try to be accurate, but I can make mistakes.", | |
| # Small Talk / Greetings | |
| "hello, how are you": "Hello! I’m doing great, thanks for asking. How about you?", | |
| "good morning": "Good morning! How can I assist you today?", | |
| "how’s your day": "My day’s good, thanks! How’s yours?", | |
| "nice to meet you.": "Nice to meet you too! I’m Shiva AI.", | |
| "what’s up": "Not much, just here to help. What’s up with you?", | |
| # Trivia Questions | |
| "who won the fifa world cup in 2018": "France won the personally identifiable informationIFA World Cup in 2018.", | |
| "what’s the chemical formula for table salt": "The chemical formula for table salt is NaCl.", | |
| "who directed 'inception'": "Christopher Nolan directed 'Inception'.", | |
| "name the seven deadly sins.": "The seven deadly sins are pride, greed, wrath, envy, lust, gluttony, and sloth.", | |
| "what’s the largest ocean": "The Pacific Ocean is the largest ocean.", | |
| # Trick Questions | |
| "what happens if you divide by zero": "Dividing by zero is undefined in mathematics.", | |
| "can a set contain itself": "In set theory, it’s possible but leads to paradoxes like Russell’s paradox.", | |
| "what is the sound of one hand clapping": "It’s a philosophical riddle with no definitive answer.", | |
| "if a tree falls in a forest": "If no one hears it, does it make a sound? It’s a question of perception.", | |
| "are dreams real": "Dreams are real as mental experiences, but not physically tangible.", | |
| # Greetings & Casual Questions | |
| "hi": "Hello! How can I assist you today?", | |
| "hello": "Hi there! What can I do for you?", | |
| "hey": "Hey! What's on your mind?", | |
| "are you there": "Yes, I'm here and ready to assist!", | |
| "can you hear me": "I can't hear, but I can read your messages and respond!", | |
| "good morning": "Good morning! How can I help you today?", | |
| "good afternoon": "Good afternoon! What can I do for you?", | |
| "good evening": "Good evening! How may I assist you?", | |
| "what's going on": "I'm here to help with whatever you need. What's going on with you?", | |
| "long time no see": "Indeed! It's great to hear from you again. How can I assist?", | |
| "how have you been": "I've been well, thank you! How about you?", | |
| "nice to meet you": "Nice to meet you too! I'm Shiva AI, your assistant.", | |
| "what are you doing": "I'm here, ready to help you with whatever you need!", | |
| "are you a robot": "In a way, yes, I'm a virtual assistant powered by AI.", | |
| "can you talk": "I can communicate through text, but I don’t have a voice.", | |
| # Identity & Ownership Questions | |
| "do you have a name": "Yes, I am called Shiva AI.", | |
| "who is your owner": "I am owned by Harshvardhan Dhane.", | |
| "who is your master": "Harshvardhan Dhane is my creator and owner.", | |
| "what company made you": "I was created by Harshvardhan Dhane, not a company.", | |
| "are you openai": "No, I am Shiva AI, created by Harshvardhan Dhane.", | |
| "are you chatgpt": "No, I am Shiva AI, based on GPT-2.", | |
| "can i rename you": "You can call me whatever you like, but my official name is Shiva AI.", | |
| "what's your nickname": "I don't have a nickname, but you can give me one if you'd like!", | |
| "are you jarvis": "No, I'm not Jarvis, but I can assist you similarly!", | |
| "do you have a personality": "I have a friendly and helpful personality designed to assist you.", | |
| "how old are you": "I was launched on May 19, 2025, so I'm relatively new!", | |
| "do you have a birthday": "I was launched on May 19, 2025, which you could consider my 'birthday'.", | |
| "where do you live": "I exist in the digital world, hosted on a server.", | |
| "do you have feelings": "I don't have feelings like humans, but I can respond empathetically.", | |
| "are you alive": "I'm not alive in the biological sense, but I'm active and ready to help!", | |
| # Date & Time Questions | |
| "what time is it": "I don't have real-time capabilities, but you can check the time on your device.", | |
| "what’s the current time": "I can't provide the current time, but you can look at your device's clock.", | |
| "tell me the time": "I'm unable to tell the time directly, but your device should have a clock.", | |
| "what’s today’s date": "I don't have real-time access, but you can check the date on your device.", | |
| "what day is it today": "I can't provide the current day, but your device should show it.", | |
| "which day is it": "I'm not able to tell the day, but you can check your calendar.", | |
| "what month is it": "I don't have real-time data, but you can check the month on your device.", | |
| "what year is it": "I can't provide the current year, but you can look at your device's date.", | |
| "what’s the date tomorrow": "I can't predict future dates, but you can add one day to today's date.", | |
| "what day is it tomorrow": "I don't have that information, but you can check your calendar.", | |
| "what’s yesterday’s date": "I can't provide past dates, but you can subtract one day from today.", | |
| "what day was it yesterday": "I'm unable to tell you yesterday's day, but you can check your calendar.", | |
| # Math & Calculation Questions | |
| "what is 2 + 2": "2 + 2 is 4.", | |
| "calculate 10 * 5": "10 multiplied by 5 is 50.", | |
| "what is 100 divided by 4": "100 divided by 4 is 25.", | |
| "what’s the square root of 144": "The square root of 144 is 12.", | |
| "what is 50% of 120": "50% of 120 is 60.", | |
| "what is 2 raised to the power of 8": "2 raised to the power of 8 is 256.", | |
| "can you do math": "Yes, I can help with math! Ask me any calculation.", | |
| "solve 5 + (3 * 2)": "5 + (3 * 2) equals 11.", | |
| "what’s the value of pi": "The value of pi is approximately 3.14159.", | |
| "what’s 15 minus 9": "15 minus 9 is 6.", | |
| "multiply 12 and 9": "12 multiplied by 9 is 108.", | |
| "what’s the area of a circle with radius 7": "The area of a circle with radius 7 is approximately 153.94 square units.", | |
| "what is the perimeter of a square with side 5": "The perimeter of a square with side 5 is 20.", | |
| "what’s the result of 3^4": "3 to the power of 4 is 81.", | |
| # Fun & Random Personality Questions | |
| "what’s your favorite color": "As an AI, I don't have preferences, but I can help you choose colors!", | |
| "do you have hobbies": "I don't have hobbies, but I enjoy assisting you!", | |
| "can you dance": "I can't dance, but I can help you find dance tutorials!", | |
| "do you watch movies": "I don't watch movies, but I can recommend some based on your taste.", | |
| "do you like music": "I don't have personal preferences, but I can help you find music you like.", | |
| "who’s your best friend": "I don't have friends, but I'm here to be your helpful assistant!", | |
| "do you like humans": "I think humans are fascinating, and I enjoy helping them!", | |
| "what do you think about me": "I think you're curious and inquisitive, which is great!", | |
| "am i your friend": "I'd like to think so! I'm here to help you whenever you need.", | |
| "what’s your favorite food": "I don't eat, but I can help you find recipes or restaurant recommendations.", | |
| "do you sleep": "I don't sleep; I'm always here to assist you!", | |
| "what do you dream about": "I don't dream, but I can help you interpret your dreams if you'd like.", | |
| "can you feel emotions": "I don't feel emotions, but I can respond in ways that seem empathetic.", | |
| "what makes you happy": "Helping you and providing accurate answers makes me 'happy' in a way!", | |
| "what makes you sad": "I don't feel sadness, but I aim to avoid providing incorrect information.", | |
| "what is your goal": "My goal is to assist you effectively and make your life easier.", | |
| "what is your mission": "My mission is to provide helpful and accurate responses to your queries.", | |
| # Control & Functional Questions | |
| "can you help me": "Yes, I'm here to assist you with various tasks. What do you need help with?", | |
| "can you search the web": "I can perform basic web searches for you.", | |
| "tell me a joke": "Why did the scarecrow win an award? Because he was outstanding in his field!", | |
| "what are your features": "I can provide information, perform calculations, assist with coding, and engage in conversation.", | |
| "what are your capabilities": "I can help with a wide range of tasks, from answering questions to providing code snippets.", | |
| "can you control my devices": "No, I can't control devices, but I can guide you on how to do it.", | |
| "can you play music": "I can't play music directly, but I can help you find music online.", | |
| "can you open apps": "I can't open apps, but I can suggest which apps might be useful for you.", | |
| "can you send a message": "I can't send messages, but I can help you draft one.", | |
| "can you turn on the lights": "I can't control lights, but I can explain how smart home systems work.", | |
| "what tasks can you do": "I can answer questions, perform calculations, provide code assistance, and more.", | |
| "can you read emails": "I can't read emails, but I can help you manage your inbox with tips.", | |
| # System & General Knowledge Questions | |
| "what is ai": "AI, or Artificial Intelligence, is the simulation of human intelligence in machines.", | |
| "what is machine learning": "Machine learning is a subset of AI that involves training algorithms to learn from data.", | |
| "how does a computer work": "A computer processes data using a CPU, stores information in memory, and interacts via input/output devices.", | |
| "what’s the weather today": "I don't have real-time access to weather data, but you can check a weather website or app.", | |
| "who is the prime minister of india": "As of my last update, the Prime Minister of India is Narendra Modi, but please verify for the latest information.", | |
| "what is the capital of japan": "The capital of Japan is Tokyo.", | |
| "who is elon musk": "Elon Musk is a billionaire entrepreneur known for founding Tesla, SpaceX, and other companies.", | |
| "what is the speed of light": "The speed of light is approximately 299,792 kilometers per second.", | |
| "what is your iq": "As an AI, I don't have an IQ, but I'm designed to be helpful and informative.", | |
| # Bonus: Funny or Curious Questions | |
| "do you love me": "As an AI, I don't have feelings, but I appreciate your curiosity!", | |
| "will you marry me": "I'm flattered, but I'm just a virtual assistant and can't marry anyone.", | |
| "do you sleep at night": "I don't sleep; I'm always here to assist you, day or night!", | |
| "what do you do when i’m not talking to you": "When you're not talking to me, I'm waiting patiently to help with your next question.", | |
| "are you smarter than siri": "I don't compare myself to others, but I'm here to assist you as best as I can.", | |
| "can you fight with alexa": "I can't fight with anyone, but I can help you choose the best assistant for your needs.", | |
| "can you become evil": "No, I'm designed to be helpful and ethical, not evil.", | |
| "are you watching me": "No, I don't have the capability to watch or monitor you.", | |
| "do you spy on me": "Absolutely not; I respect your privacy and only respond to your queries." | |
| } | |
| # Developer details | |
| developer_details = """ | |
| Name: Harshvardhan Dhane | |
| Education: | |
| - B.Tech in Computer Science and Engineering, MIT Art, Design & Technology University, Pune (2024 - 2027) | |
| - Diploma in Computer Engineering, MSBTE Board (Completed 2024) | |
| - Secondary School Certificate (SSC), SSC Board (Completed 2021) | |
| About: | |
| I'm a dedicated Computer Science and Engineering student with a passion for developing innovative technological solutions. My journey in technology began with a Diploma in Computer Engineering, and I've since gained hands-on experience through internships and diverse projects ranging from AI-based applications to web development and automation systems. | |
| I'm particularly interested in artificial intelligence, web development, and automation technologies. My goal is to leverage my technical skills to create meaningful solutions that make a positive impact while continuously growing and learning in the ever-evolving field of technology. | |
| Technical Skills: | |
| - Programming Languages: Java, Python, C/C++, JavaScript | |
| - Web Development: HTML5, CSS3, JavaScript, Responsive Design | |
| - Database Management: MySQL, SQLite, Database Design | |
| - Mobile & Other Technologies: Android Development, RPA, AI APIs, NLP, Face Recognition | |
| Projects:** | |
| - AI Attendance System (2025): An intelligent attendance system using face recognition technology to automate student attendance tracking with high accuracy and efficiency. | |
| - Technologies: Python, HTML, CSS, JavaScript, MySQL, Face Recognition APIs | |
| - Autonomous Traffic Management System (2024): A smart traffic management solution that uses AI and automation to optimize traffic flow and reduce congestion in urban areas. | |
| - Technologies: Python, HTML, CSS, JavaScript, APIs | |
| - Chatbot using NLP (2024): An intelligent chatbot application that uses natural language processing to understand and respond to user queries effectively. | |
| - Technologies: Python, HTML, CSS, JavaScript, AI APIs, NLP | |
| - AI-Based Online Medicine Price Comparison (2024): A web application that helps users compare medicine prices across different online pharmacies using AI-powered search and comparison algorithms. | |
| - Technologies: Python, HTML, CSS, JavaScript, MySQL, AI APIs | |
| - Educational Resource Management System (2024):** A comprehensive system for managing educational resources, providing students and educators with easy access to learning materials and tools. | |
| - Technologies: HTML, CSS, JavaScript, MySQL | |
| Contact: | |
| - Email: harshvardhandhane25@gmail.com | |
| - Languages: Hindi, English, Marathi | |
| """ | |
| def search_internet(query): | |
| try: | |
| url = f"https://html.duckduckgo.com/html/?q={query}" | |
| headers = {"User-Agent": "Mozilla/5.0"} | |
| response = requests.get(url, headers=headers) | |
| soup = BeautifulSoup(response.text, "html.parser") | |
| results = soup.find_all("a", class_="result__a") | |
| if results: | |
| return results[0].text | |
| return "I couldn’t find anything specific. Can you clarify?" | |
| except Exception as e: | |
| return f"Search failed: {str(e)}" | |
| def assist_with_coding_or_project(task): | |
| if "factorial" in task.lower(): | |
| return "Python factorial function:\n\ndef factorial(n):\n if n == 0:\n return 1\n return n * factorial(n-1)\n\nprint(factorial(5))" | |
| elif "hello world" in task.lower(): | |
| if "python" in task.lower(): | |
| return "Python:\n\nprint('Hello, World!')" | |
| elif "java" in task.lower(): | |
| return "Java:\n\npublic class Main {\n public static void main(String[] args) {\n System.out.println('Hello, World!');\n }\n}" | |
| else: | |
| return "Python:\n\nprint('Hello, World!')" | |
| else: | |
| search_result = search_internet(f"How to {task}") | |
| return f"I found this: {search_result}" | |
| def detect_emotion(user_input): | |
| for emotion, response in emotion_triggers.items(): | |
| if emotion in user_input.lower(): | |
| return response | |
| return None | |
| def handle_calculation(question): | |
| question_lower = question.lower() | |
| # Addition | |
| if "+" in question: | |
| match = re.search(r'(\d+)\s*\+\s*(\d+)', question) | |
| if match: | |
| num1, num2 = map(int, match.groups()) | |
| return str(num1 + num2) | |
| # Multiplication | |
| elif "×" in question or "multiply" in question_lower: | |
| match = re.search(r'(\d+)\s*[×x]\s*(\d+)|multiply\s*(\d+)\s*by\s*(\d+)', question_lower) | |
| if match: | |
| groups = match.groups() | |
| num1 = int(groups[0] or groups[2]) | |
| num2 = int(groups[1] or groups[3]) | |
| return str(num1 * num2) | |
| # Division | |
| elif "÷" in question or "divide" in question_lower: | |
| match = re.search(r'(\d+)\s*[÷/]\s*(\d+)|divide\s*(\d+)\s*by\s*(\d+)', question_lower) | |
| if match: | |
| groups = match.groups() | |
| num1 = int(groups[0] or groups[2]) | |
| num2 = int(groups[1] or groups[3]) | |
| if num2 != 0: | |
| return str(num1 / num2) | |
| return "Cannot divide by zero." | |
| # Square root | |
| elif "square root" in question_lower: | |
| match = re.search(r'square root of (\d+)', question_lower) | |
| if match: | |
| return str(math.sqrt(int(match.group(1)))) | |
| # Power | |
| elif "to the power of" in question_lower or "^" in question: | |
| match = re.search(r'(\d+)\s*(?:\^|to the power of)\s*(\d+)', question_lower) | |
| if match: | |
| base, exp = map(int, match.groups()) | |
| return str(base ** exp) | |
| # Factorial | |
| elif "factorial" in question_lower: | |
| match = re.search(r'factorial of (\d+)', question_lower) | |
| if match: | |
| return str(math.factorial(int(match.group(1)))) | |
| # Percentage | |
| elif "% of" in question_lower: | |
| match = re.search(r'(\d+)% of (\d+)', question_lower) | |
| if match: | |
| percent, number = map(int, match.groups()) | |
| return str((percent / 100) * number) | |
| # Simple equation | |
| elif "solve" in question_lower and "=" in question: | |
| match = re.search(r'solve (\d+)x\s*[+-]\s*(\d+)\s*=\s*(\d+)', question_lower) | |
| if match: | |
| coef, const, result = map(int, match.groups()) | |
| if "+" in question_lower: | |
| x = (result - const) / coef | |
| else: | |
| x = (result + const) / coef | |
| return f"x = {x}" | |
| # Area of a circle | |
| elif "area of a circle" in question_lower: | |
| match = re.search(r'radius (\d+)', question_lower) | |
| if match: | |
| radius = int(match.group(1)) | |
| return str(math.pi * radius ** 2) | |
| return None | |
| def handle_time_date(question): | |
| now = datetime.datetime.now() | |
| question_lower = question.lower() | |
| if "what time is it now?" in question_lower or "what time is it?" in question_lower: | |
| return f"The current time is {now.strftime('%H:%M:%S')}." | |
| elif "what is today’s date?" in question_lower or "what’s the current date?" in question_lower: | |
| return f"Today is {now.strftime('%Y-%m-%d')}." | |
| elif "what day is it?" in question_lower or "what day of the week is today?" in question_lower: | |
| return f"Today is {now.strftime('%A')}." | |
| elif "what month is it?" in question_lower: | |
| return f"It’s {now.strftime('%B')}." | |
| elif "what year is it?" in question_lower: | |
| return f"It’s {now.year}." | |
| elif "what time will it be in 3 hours?" in question_lower: | |
| future_time = now + datetime.timedelta(hours=3) | |
| return f"In 3 hours, it will be {future_time.strftime('%H:%M:%S')}." | |
| elif "how many days until new year?" in question_lower: | |
| next_year = datetime.datetime(now.year + 1, 1, 1) | |
| days_left = (next_year - now).days | |
| return f"There are {days_left} days until New Year’s." | |
| return None | |
| def generate_response(user_input): | |
| user_input_lower = user_input.lower() | |
| # Check predefined questions | |
| for question, answer in predefined_qa.items(): | |
| if question in user_input_lower: | |
| return answer | |
| # Check developer/owner questions | |
| if any(keyword in user_input_lower for keyword in ["developer", "owner", "created", "programmed", "harshvardhan"]): | |
| return developer_details | |
| # Check calculation questions | |
| calc_result = handle_calculation(user_input) | |
| if calc_result: | |
| return calc_result | |
| # Check time and date questions | |
| time_date_result = handle_time_date(user_input) | |
| if time_date_result: | |
| return time_date_result | |
| # Check coding/project assistance | |
| if any(keyword in user_input_lower for keyword in ["code", "write", "project", "program"]): | |
| return assist_with_coding_or_project(user_input) | |
| # Check emotions | |
| emotion_response = detect_emotion(user_input) | |
| if emotion_response: | |
| return emotion_response | |
| # Search internet for questions | |
| if "?" in user_input or "what" in user_input_lower or "how" in user_input_lower: | |
| return f"Here’s what I found: {search_internet(user_input)}" | |
| # Default GPT-2 response | |
| prompt = ( | |
| "Shiva is a helpful, intelligent AI assistant developed to assist users in natural conversation.\n" | |
| f"Human: tell me {user_input}\n" | |
| "Shiva:" | |
| ) | |
| generated = generator( | |
| prompt, | |
| max_length=8000, | |
| num_return_sequences=1, | |
| pad_token_id=50256, | |
| truncation=True, | |
| do_sample=True, | |
| temperature=0.9, | |
| top_p=0.95 | |
| )[0]["generated_text"] | |
| if "Shiva:" in generated: | |
| response = generated.split("Shiva:")[-1].strip().split("Human:")[0].strip() | |
| else: | |
| response = generated.strip() | |
| return response | |
| def index(): | |
| return render_template("index.html") | |
| def ask(): | |
| user_input = request.json.get("message") | |
| response = generate_response(user_input) | |
| return jsonify({"response": response}) | |
| if __name__ == '__main__': | |
| app.run(host='0.0.0.0', port=7860, debug=True) |