Skip to content

Commit 5ae23a1

Browse files
authored
Merge branch 'avinashkranjan:master' into mosaic
2 parents 3610332 + c0b68a7 commit 5ae23a1

File tree

103 files changed

+3994
-230
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+3994
-230
lines changed

.github/ChatBot/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Rule Based Chatbot using NLTK
2+
3+
This is a simple rule based chatbot implemented using the Natural Language Toolkit (NLTK) library in Python. The chatbot engages in conversations with users by matching their input with predefined patterns and providing appropriate responses.
4+
5+
## How it Works
6+
7+
The chatbot is built using NLTK's `Chat` class, which allows us to define patterns and corresponding responses. When the user inputs a message, the chatbot searches for a pattern that matches the input and responds with the associated response. If there's no direct match, the chatbot provides a default response to handle any unrecognized inputs.
8+
9+
The predefined patterns cover a range of greetings, questions about the chatbot, jokes, recommendations, and handling user intentions. The chatbot is designed to respond politely and engagingly to various types of user queries.
10+
11+
## Usage
12+
13+
1. Run the `chatbot.py` script, and the chatbot will greet you with a welcome message.
14+
2. Enter your message or query, and the chatbot will respond accordingly.
15+
3. To stop the conversation, simply type `quit` as your input, and the chatbot will bid you goodbye.
16+
17+
## Improving the Chatbot
18+
19+
The predefined patterns can be expanded to make the chatbot more engaging and diverse. In the `chat_patterns.py` file, you can add more patterns and responses to handle a broader range of queries. By doing so, you can reduce the likelihood of default responses and create a more dynamic and interactive chatbot.
20+
21+
## Dependencies
22+
23+
To run the chatbot, ensure you have the following installed:
24+
25+
- Python 3.x
26+
- NLTK library
27+
28+
You can install the required dependencies using pip:
29+
30+
```bash
31+
pip install nltk

.github/ChatBot/chat_patterns.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Predefined patterns and responses for the chatbot
2+
3+
patterns = [
4+
# Greetings
5+
(r'hi|hello|hey', ['Hello!', 'Hi!', 'Hey!', 'Hi there!']),
6+
(r'how are you?', ['I am good, thank you!',
7+
'I am doing well!', 'All good!', 'I\'m fine, thanks!']),
8+
(r'what is your name?', ['You can call me Chatbot.',
9+
'I am Chatbot!', 'My name is Chatbot.']),
10+
(r'bye|goodbye', ['Goodbye!', 'See you later!',
11+
'Bye!', 'Have a great day!']),
12+
13+
# Jokes
14+
(r'tell me a joke', ['Why don’t scientists trust atoms? Because they make up everything!',
15+
'Parallel lines have so much in common. It’s a shame they’ll never meet.',
16+
'Why did the scarecrow win an award? Because he was outstanding in his field!']),
17+
18+
# Age
19+
(r'how old are you?', ['I am a computer program, so I don\'t have an age!',
20+
'Age is just a number, and I don\'t have one!']),
21+
22+
# Creator
23+
(r'who created you?', ['I was created by OpenAI.',
24+
'My creators are from OpenAI.']),
25+
26+
# Compliments
27+
(r'(.*) (like|love) you', ["Aw, that's so sweet!",
28+
"Thank you! I really appreciate it."]),
29+
(r'you are (.*)(good|awesome|amazing)',
30+
["Thank you! I'm here to assist you.", "I'm glad you think so!"]),
31+
32+
# Weather
33+
(r'(.*) (weather|temperature) today', ["I'm sorry, I am just a chatbot and don't have access to real-time data.",
34+
"You can check the weather online or through a weather app."]),
35+
36+
# Recommendations
37+
(r'(.*) (movie|book) (recommendation|recommend)', ["I recommend you watch 'The Shawshank Redemption' or read 'To Kill a Mockingbird'.",
38+
"You might enjoy 'Inception' or 'The Great Gatsby'.",
39+
"If you like action, 'The Dark Knight' is a great choice."]),
40+
41+
# How to create/build something
42+
(r'how (can|do) (I|you) (create|build) (a|an) (.*)', ["To create {4}, you can follow these steps...",
43+
"Building {4} requires some technical knowledge, but here are the basics...",
44+
"Sure! Here's a basic guide on building {4}..."]),
45+
46+
# User intentions
47+
(r'I (want|need) (.*)',
48+
["Why do you need {1}?", "What would you do with {1}?"]),
49+
(r'I am (feeling|looking) (.*)',
50+
["Why are you feeling {1}?", "Tell me more about why you are {1}."]),
51+
52+
# More patterns and responses to handle different queries
53+
# Add more patterns and responses here to make the chatbot more engaging and diverse
54+
55+
# Default response
56+
(r'.*', ["I'm sorry, I don't quite understand. Could you please rephrase that?",
57+
"I'm still learning, and I'm not sure how to respond to that.",
58+
"Let's talk about something else. What else would you like to know?"]),
59+
]

.github/ChatBot/chatbot.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import nltk
2+
from nltk.chat.util import Chat, reflections
3+
# Import patterns from the chat_patterns.py file
4+
from chat_patterns import patterns
5+
6+
chatbot = Chat(patterns, reflections)
7+
8+
9+
def start_chat():
10+
print("Chatbot: Hi! How can I help you today?")
11+
while True:
12+
user_input = input("You: ")
13+
if user_input.lower() == 'quit':
14+
print("Chatbot: Goodbye!")
15+
break
16+
response = chatbot.respond(user_input)
17+
print("Chatbot:", response)
18+
19+
20+
if __name__ == "__main__":
21+
nltk.download('punkt')
22+
start_chat()

AI Based Music Composer/README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# AI-based Music Composer
2+
3+
This project is an AI-based music composer that generates unique pieces of music based on user preferences or specific genres. The script uses the `magenta` library, which provides pre-trained models for music generation.
4+
5+
## Table of Contents
6+
- [Introduction](#introduction)
7+
- [Features](#features)
8+
- [Installation](#installation)
9+
- [Usage](#usage)
10+
- [User Input](#user-input)
11+
- [Chord Progressions](#chord-progressions)
12+
- [Drum Accompaniment](#drum-accompaniment)
13+
- [Generated Music](#generated-music)
14+
- [Contributing](#contributing)
15+
16+
## Introduction
17+
18+
Music is an integral part of human expression, and this project aims to create an AI-based music composer that can generate melodies in various genres. The generated music can be saved as MIDI files and can serve as a starting point for further music composition and creativity.
19+
20+
## Features
21+
22+
- Generates unique melodies based on pre-trained models.
23+
- Allows user input for preferred genre and tempo.
24+
- Adds chord progressions based on the chosen genre to create harmonic accompaniments.
25+
- Includes a simple drum pattern for rhythmic accompaniment.
26+
- Outputs generated music as MIDI files.
27+
28+
## Installation
29+
30+
1. Clone the repository to your local machine:
31+
32+
2. Install the required dependencies:
33+
34+
```bash
35+
pip install magenta
36+
```
37+
38+
## Usage
39+
To generate music pieces using the AI-based music composer, run the following command:
40+
41+
```bash
42+
python music_composer.py
43+
```
44+
The script will prompt you to enter your preferred genre and tempo (BPM) for music generation.
45+
46+
## User Input
47+
The script allows you to input your preferred genre, which can be one of the following:
48+
49+
- Classical
50+
- Jazz
51+
- Rock
52+
Additionally, you can specify the tempo (in BPM) for the generated music.
53+
54+
## Chord Progressions
55+
The chosen genre determines the chord progression for the generated music. Currently, the supported chord progressions are:
56+
57+
- Classical: ["C", "Am", "F", "G"]
58+
- Jazz: ["Cmaj7", "Dm7", "Em7", "A7"]
59+
- Rock: ["C", "G", "Am", "F"]
60+
You can modify or extend these chord progressions in the music_composer.py file.
61+
62+
## Drum Accompaniment
63+
The music pieces include a basic drum pattern for rhythmic accompaniment. The drum pattern consists of a kick drum (36) and hi-hat (42). You can adjust the drum pattern in the music_composer.py file to create different rhythms.
64+
65+
## Generated Music
66+
The generated music will be saved as MIDI files in the generated_music directory. Each music piece will have a unique file name, such as music_piece_1.mid, music_piece_2.mid, and so on.
67+
68+
## Contributing
69+
If you have any ideas, improvements, or bug fixes, feel free to open an issue or submit a pull request. We appreciate your contributions!
70+
71+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import os
2+
import magenta.music as mm
3+
from magenta.models.melody_rnn import melody_rnn_sequence_generator
4+
5+
# Set the directory to save the generated MIDI files
6+
output_dir = 'generated_music'
7+
os.makedirs(output_dir, exist_ok=True)
8+
9+
# Initialize the Melody RNN model
10+
model_name = 'attention_rnn'
11+
melody_rnn = melody_rnn_sequence_generator.MelodyRnnSequenceGenerator(
12+
model_name=model_name)
13+
14+
# Set the temperature for music generation (higher values lead to more randomness)
15+
temperature = 1.0
16+
17+
# Set the number of music pieces to generate
18+
num_music_pieces = 3
19+
20+
# Set the number of steps per music piece
21+
steps_per_music_piece = 128
22+
23+
# User input for preferred genre and tempo
24+
preferred_genre = input(
25+
"Enter your preferred genre (e.g., classical, jazz, rock): ")
26+
preferred_tempo = int(input("Enter your preferred tempo (BPM): "))
27+
28+
# Chord progression for the chosen genre (you can add more genres and progressions)
29+
chord_progressions = {
30+
"classical": ["C", "Am", "F", "G"],
31+
"jazz": ["Cmaj7", "Dm7", "Em7", "A7"],
32+
"rock": ["C", "G", "Am", "F"],
33+
}
34+
35+
# Basic drum pattern for accompaniment
36+
drum_pattern = mm.DrumTrack(
37+
# Kick drum and Hi-hat pattern (adjust as needed)
38+
[36, 0, 42, 0, 36, 0, 42, 0],
39+
start_step=0,
40+
steps_per_bar=steps_per_music_piece // 4,
41+
steps_per_quarter=4,
42+
)
43+
44+
# Generate music pieces
45+
for i in range(num_music_pieces):
46+
# Generate a melody sequence
47+
melody_sequence = melody_rnn.generate(
48+
temperature=temperature,
49+
steps=steps_per_music_piece,
50+
primer_sequence=None
51+
)
52+
53+
# Add chords to the melody sequence based on the preferred genre
54+
chords = [chord_progressions.get(preferred_genre, ["C"])[i % len(
55+
chord_progressions.get(preferred_genre, ["C"]))] for i in range(steps_per_music_piece)]
56+
chord_sequence = mm.ChordSequence(chords)
57+
melody_with_chords_sequence = mm.sequences_lib.concatenate_sequences(
58+
melody_sequence, chord_sequence)
59+
60+
# Create a MIDI file from the melody with chords sequence and drum pattern
61+
music_sequence = mm.sequences_lib.concatenate_sequences(
62+
melody_with_chords_sequence, drum_pattern)
63+
music_sequence.tempos[0].qpm = preferred_tempo
64+
65+
midi_file = os.path.join(output_dir, f'music_piece_{i + 1}.mid')
66+
mm.sequence_proto_to_midi_file(music_sequence, midi_file)
67+
print(f'Music piece {i + 1} generated and saved as {midi_file}')
68+
69+
print('Music generation complete!')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
magenta

AI_Guess_a_number/Guess_a_number.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
import random
3+
4+
5+
def play_game():
6+
print("Welcome to the Number Guessing Game!")
7+
print("Think of a number between 1 and 100, and I will try to guess it.")
8+
print("Press 'y' if my guess is correct, 'l' if your number is lower, or 'h' if your number is higher.")
9+
10+
low = 1
11+
high = 100
12+
guess_count = 0
13+
correct_guess = False
14+
15+
while not correct_guess:
16+
guess = random.randint(low, high)
17+
print("Is your number", guess, "?")
18+
user_input = input("Enter 'y', 'l', or 'h': ")
19+
20+
if user_input == "y":
21+
print("I guessed it! Your number is", guess)
22+
print("I made", guess_count, "guesses.")
23+
correct_guess = True
24+
elif user_input == "l":
25+
high = guess - 1
26+
guess_count += 1
27+
elif user_input == "h":
28+
low = guess + 1
29+
guess_count += 1
30+
else:
31+
print("Invalid input. Please try again.")
32+
33+
34+
play_game()

AI_Guess_a_number/README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
Package/Script Name: Multiplayer Guess_a_number Game
2+
3+
Short description of package/script:
4+
This package contains a simple implementation of an online multiplayer "Guess the Number" game using Python's socket module. Players can connect to a server and take turns guessing a randomly generated number between 1 and 100. The game will continue until one of the players guesses the correct number.
5+
6+
Functionalities:
7+
8+
Allow multiple players to connect to the server
9+
Generate a random number between 1 and 100 for players to guess
10+
Check each player's guess and provide feedback (higher or lower)
11+
Declare the winner when a player guesses the correct number
12+
Allow players to quit the game at any time
13+
Setup instructions:
14+
15+
Make sure you have Python installed on your system.
16+
Download the script and save it with the name "server.py" for the server-side code and "client.py" for the client-side code.
17+
Explain how to set up and run your package/script on the user's system:
18+
19+
Open a terminal or command prompt.
20+
Navigate to the directory where you saved "server.py" and "client.py".
21+
To run the server:
22+
3. Run the following command:
23+
24+
Copy code
25+
python server.py
26+
The server will start listening for client connections.
27+
28+
To run the client:
29+
4. Open a new terminal or command prompt window.
30+
31+
Run the following command:
32+
Copy code
33+
python client.py
34+
The client will connect to the server, and the game will start.
35+
Follow the on-screen instructions to play the game. Enter your guess or type "quit" to leave the game.
36+
Detailed explanation of the script:
37+
38+
The server script initializes a socket and listens for incoming connections from clients.
39+
When the first client connects, the server generates a random number between 1 and 100 and sends it to all connected clients.
40+
The server waits for guesses from clients and provides feedback on whether the guess is higher or lower than the target number.
41+
If a client guesses the correct number, the server announces the winner, and the game ends.
42+
If a client sends "quit," they will be disconnected from the game.
43+
The client script connects to the server and interacts with the user to play the game. It sends the user's guesses to the server and displays the feedback received from the server until the correct number is guessed or the user quits the game.
44+
45+
Output:
46+
Below is an example of how the output might look while running the game:
47+
48+
Server terminal:
49+
50+
sql
51+
Copy code
52+
Server started. Waiting for players...
53+
New connection from 127.0.0.1:12345
54+
New connection from 127.0.0.1:23456
55+
Game started!
56+
Guess a number between 1 and 100.
57+
Client terminal 1 (Player 1):
58+
59+
vbnet
60+
Copy code
61+
Connected to the server.
62+
Guess the number: 50
63+
Your guess is too low. Try again.
64+
Guess the number: 75
65+
Your guess is too high. Try again.
66+
Guess the number: 62
67+
Your guess is too high. Try again.
68+
Guess the number: 56
69+
Congratulations! You guessed the number.
70+
Client terminal 2 (Player 2):
71+
72+
vbnet
73+
Copy code
74+
Connected to the server.
75+
Guess the number: 40
76+
Your guess is too low. Try again.
77+
Guess the number: 60
78+
Your guess is too low. Try again.
79+
Guess the number: 70
80+
Your guess is too high. Try again.
81+
Guess the number: 56
82+
Player 1 has won. The number was 56.
83+
84+
Author:Shikhar9425
85+
86+
Disclaimers:
87+
This script is a simplified example intended for educational and illustrative purposes only. It is not a complete or production-ready implementation of a multiplayer game. For a real-world application, additional considerations, such as security, scalability, and error handling, would need to be addressed.
88+
89+
Please note that running the server script opens a network port on your machine, and it may be accessible to other devices on the local network. In a production environment, appropriate security measures must be taken to protect the server and the network from potential vulnerabilities.

0 commit comments

Comments
 (0)