0% found this document useful (0 votes)
7 views

Python Open Ended Code

The document contains code for an AI assistant that processes voice commands using speech recognition and various APIs for functionalities like playing music, fetching weather, and telling jokes. It includes modules for intent detection, audio handling, and conversation management, utilizing libraries such as FastAPI and pandas. The assistant can set reminders and alarms, and it manages delayed messages for reminders through threading.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Python Open Ended Code

The document contains code for an AI assistant that processes voice commands using speech recognition and various APIs for functionalities like playing music, fetching weather, and telling jokes. It includes modules for intent detection, audio handling, and conversation management, utilizing libraries such as FastAPI and pandas. The assistant can set reminders and alarms, and it manages delayed messages for reminders through threading.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

ASSISTANT.

PY

import speech_recognition as sr
import wikipedia
import datetime
import pyjokes
import pywhatkit
import webbrowser
import requests
import random
import threading
import time
import re
from intent_detector import detect_intent
from datetime import datetime, timedelta
import pandas as pd
import random
import ast

df_pa�erns = pd.read_csv('df_pa�erns.csv')
df_responses = pd.read_csv('df_responses.csv')

delayed_messages = []
recognizer = sr.Recognizer()

def recognize_audio(file_path):
with sr.AudioFile(file_path) as source:
audio_data = recognizer.record(source)
command = recognizer.recognize_google(audio_data)
return command.lower()

def process_command(command: str):


try:
intent = detect_intent(command)
print(f"[Intent]: {intent}")

if intent == "wikipedia_search":
return duckduckgo_search(command)
elif intent == "play_music":
song = command.replace("play", "").strip()
pywhatkit.playonyt(song)
return f"Playing {song} on YouTube."

elif intent == "get_time":


time = datetime.now().strftime('%I:%M %p')
return f"The current time is {time}"

elif intent == "get_date":


date = datetime.now().strftime('%B %d, %Y')
return f"Today's date is {date}"

elif intent == "tell_joke":


return pyjokes.get_joke()

elif intent == "open_website":


site = command.replace("open", "").strip()
url = f"h�ps://{site}.com"
webbrowser.open(url)
return f"Opening {site}"

elif intent == "get_weather":


return get_weather(command)

elif intent == "get_news":


return get_news()

elif intent == "fun_fact":


return get_fun_fact()

elif intent == "casual_talk":


return casual_response(command)
elif intent == "set_reminder":
return set_reminder(command)

elif intent == "set_alarm":


return set_alarm(command)

else:
return "I couldn't understand the command. Please try again."

except Exception as e:
return f"Error occurred: {str(e)}"

def casual_response(command: str):


command = command.lower()

matched_intent = None
for _, row in df_pa�erns.iterrows():
try:
pa�erns = ast.literal_eval(row['text']) # Safely parse list string
if any(pa�ern.lower() in command for pa�ern in pa�erns):
matched_intent = row['intent']
break
except Exception as e:
print("Pa�ern parsing error:", e)

if matched_intent:
response_row = df_responses[df_responses['intent'] == matched_intent]
if not response_row.empty:
try:
responses = ast.literal_eval(response_row['responses'].values[0])
return random.choice(responses)
except Exception as e:
print("Response parsing error:", e)
return "Sorry, I couldn't generate a response right now."
else:
fallback_responses = [
"That's interesting!",
"I'm here if you need anything.",
"Sounds good!",
"Let's do something fun!"
]
return random.choice(fallback_responses)

import requests

def get_news():
try:
url =
"h�ps://gnews.io/api/v4/top-headlines?lang=en&country=in&max=3&apikey=2dd3dfcd95f7dcef71a5dd9ce499d
37c"
response = requests.get(url)
data = response.json()
articles = data.get("articles", [])

if not articles:
return "No news available at the moment."

news_summary = []
for i, article in enumerate(articles):
title = article.get("title", "No title")
description = article.get("description", "No description")
source = article.get("source", {}).get("name", "Unknown Source")

forma�ed = (
f"{i+1}. {title}\n"
f" {source}\n"
f" {description}\n"

)
news_summary.append(forma�ed)

return " Top News:\n\n" + "\n".join(news_summary)


except Exception as e:
return f"Failed to fetch news: {str(e)}"

def get_fun_fact():
try:
response = requests.get("h�ps://uselessfacts.jsph.pl/random.json?language=en")
data = response.json()
return f"Here's a fun fact: {data.get('text', 'Could not fetch a fact at this moment.')}"
except Exception as e:
return f"Failed to fetch fun fact: {str(e)}"

def duckduckgo_search(query):
try:
params = {
"q": query,
"format": "json",
"no_html": 1,
"skip_disambig": 1
}
response = requests.get("h�ps://api.duckduckgo.com/", params=params)
data = response.json()

abstract = data.get("Abstract")
if abstract:
return abstract
elif data.get("RelatedTopics"):
for topic in data["RelatedTopics"]:
if "Text" in topic:
return topic["Text"]
return "I couldn't find a good answer, but you can try searching it online."
except Exception as e:
return f"Failed to fetch information: {str(e)}"

def set_reminder(command: str):


match = re.search(r"remind me to (.+?) in (\d+) (second|seconds|minute|minutes)", command)
if not match:
return "Please specify the reminder and duration like 'Remind me to drink water in 10 minutes'."

task = match.group(1)
duration = int(match.group(2))
unit = match.group(3)

delay = duration * 60 if "minute" in unit else duration

def remind():
time.sleep(delay)
delayed_messages.append(f" Reminder: Don't forget to {task}!")

threading.Thread(target=remind).start()
return f"Okay, I will remind you to {task} in {duration} {unit}."

import threading
import time
from datetime import datetime, timedelta
import re
from playsound import playsound # Make sure playsound is installed: pip install playsound

def set_alarm(command: str):


# Match formats like "4:18 pm", "4:18 p.m.", "04:18 AM", etc.
match = re.search(r"set an alarm for (\d{1,2}):(\d{2}) ?(a\.?m\.?|p\.?m\.?)", command, re.IGNORECASE)
if not match:
return "Please specify the alarm time like 'Set an alarm for 7:30 am'."

hour, minute = int(match.group(1)), int(match.group(2))


period = match.group(3).replace(".", "").lower() # Normalize to "am" or "pm"

if period == "pm" and hour != 12:


hour += 12
elif period == "am" and hour == 12:
hour = 0
now = datetime.now()
alarm_time = now.replace(hour=hour, minute=minute, second=0, microsecond=0)

if alarm_time <= now:


alarm_time += timedelta(days=1)

seconds_until_alarm = (alarm_time - now).total_seconds()

def alarm():
time.sleep(seconds_until_alarm)
delayed_messages.append(" Alarm ringing! Time to wake up!")
start_time = time.time()
duration = 60 # Repeat for 1 minute
while time.time() - start_time < duration:
try:
playsound("alarm.mp3")
except Exception as e:
print(f"Error playing sound: {e}")
break

threading.Thread(target=alarm).start()
return f"Alarm set for {alarm_time.strftime('%I:%M %p')}."

def get_weather(command):
try:
# Extract city name from command using regex like "weather in Mysore"
match = re.search(r"(?:weather\s*(?:in|at)?\s*)([a-zA-Z\s]+)", command)
if match:
city = match.group(1).strip()
else:
# Try fallback: extract city using common phrases
match = re.search(r"(?:in|at)\s+([a-zA-Z\s]+)", command)
if match:
city = match.group(1).strip()
else:
return "Please specify the city name like 'weather in Chennai'."
api_key = "2f400256790f47b64cb1fdc7c8876ea7" # Replace with real key
url = f"h�ps://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
response = requests.get(url)
data = response.json()

if data.get("cod") != 200:
return f"Couldn't fetch weather for {city}. Please check the city name."

weather_desc = data["weather"][0]["description"]
temp = data["main"]["temp"]
feels_like = data["main"]["feels_like"]
humidity = data["main"]["humidity"]
wind = data["wind"]["speed"]

return (
f" Weather in {city.title()}:\n"
f"• Description: {weather_desc.capitalize()}\n"
f"• Temperature: {temp}°C (Feels like {feels_like}°C)\n"
f"• Humidity: {humidity}%\n"
f"• Wind Speed: {wind} m/s"
)
except Exception as e:
return f"Failed to fetch weather: {str(e)}"

INTENTS.PY

# intents.py
import pandas as pd
import ast

df = pd.read_csv('df_pa�erns.csv')
casual_talk_pa�erns = []
for item in df['text']:
try:
pa�erns = ast.literal_eval(item)
casual_talk_pa�erns.extend(pa�erns)
except:
continue

INTENTS = {
"get_time": ["what time", "current time", "tell me the time"],
"get_date": ["what date", "today's date", "current date"],
"play_music": ["play", "play song", "play music"],
"tell_joke": ["joke", "make me laugh", "tell me something funny"],
"fun_fact": ["fun fact", "interesting fact", "something cool"],
"get_news": ["news", "headlines", "latest news"],
"get_weather": ["weather", "temperature", "climate"],
"casual_talk": casual_talk_pa�erns,
"wikipedia_search": ["who is", "what is", "tell me about", "where is", "how does"],
"open_website": ["open"],

"set_reminder": ["remind me", "set a reminder", "remember to"],


"set_alarm": ["set an alarm", "wake me up", "alarm for"]
}

INTENT_DETECTOR.PY

from intents import INTENTS

def detect_intent(command):
command = command.lower()
for intent, keywords in INTENTS.items():
for keyword in keywords:
if keyword in command:
return intent
return "unknown"
MAIN.PY

from fastapi import FastAPI, UploadFile, File


from fastapi.middleware.cors import CORSMiddleware
from assistant import recognize_audio, process_command
import os
import shutil
import uuid
from pydub import AudioSegment

app = FastAPI()

app.add_middleware(
CORSMiddleware,
allow_origins=["h�p://localhost:5008"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)

UPLOAD_FOLDER = "audio_files"
os.makedirs(UPLOAD_FOLDER, exist_ok=True)

@app.post("/voice-command/")
async def handle_audio(file: UploadFile = File(...)):
file_id = str(uuid.uuid4())
original_path = os.path.join(UPLOAD_FOLDER, f"{file_id}_original.webm")
converted_path = os.path.join(UPLOAD_FOLDER, f"{file_id}.wav")

# Save uploaded file


with open(original_path, "wb") as bu�er:
shutil.copyfileobj(file.file, bu�er)

try:
# Convert to WAV
audio = AudioSegment.from_file(original_path)
audio.export(converted_path, format="wav")

# Recognize audio
command = recognize_audio(converted_path)
print(f"Recognized: {command}")

# Process command using intent detection


response = process_command(command)
print(f"Response: {response}")

return {"recognized": command, "response": response}

except Exception as e:
print("Error:", e)
return {"error": str(e)}

finally:
# Clean up temporary files
if os.path.exists(original_path):
os.remove(original_path)
if os.path.exists(converted_path):
os.remove(converted_path)

from assistant import delayed_messages

@app.get("/get-delayed-messages")
def get_delayed_messages():
global delayed_messages
messages = delayed_messages[:]
delayed_messages.clear()
return {"messages": messages}
CONVERSATION.PY

import numpy as np
import json
import pandas as pd
import re
import torch
import random
import torch.nn as nn
import transformers
import gc
from transformers import BertTokenizer , BertModel
import matplotlib.pyplot as plt
device = torch.device('cuda')
import torch.nn.functional as F
from torch.optim import Adam
from tqdm import tqdm
import os

file_path = os.path.join(os.path.dirname(__file__), 'Intent.json')


with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)

class cfg:
num_classes=22
epochs=15
batch_size=10
lr=1e-5
max_length=15

df = pd.DataFrame(data['intents'])
df.head()

df_pa�erns = df[['text', 'intent']]


df_responses = df[['responses', 'intent']]
df_pa�erns.head()

print(df_pa�erns)
df_pa�erns.to_csv('df_pa�erns.csv', index=False)
df_responses.to_csv('df_responses.csv', index=False)

You might also like