Zamira1235 commited on
Commit
fc9a621
·
verified ·
1 Parent(s): 5759156

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -26
app.py CHANGED
@@ -6,12 +6,12 @@ import os
6
  os.environ["TOKENIZERS_PARALLELISM"] = "false"
7
 
8
  # Initialize paths and model identifiers for easy configuration and maintenance
9
- filename = "output_topic_details.txt" # Path to the file storing chess-specific details
10
  retrieval_model_name = 'output/sentence-transformer-finetuned/'
11
 
12
  openai.api_key = os.environ["OPENAI_API_KEY"]
13
 
14
- system_message = "You are a song chatbot specialized in providing song reccomendations based on mood."
15
  # Initial system message to set the behavior of the assistant
16
  messages = [{"role": "system", "content": system_message}]
17
 
@@ -64,14 +64,15 @@ def find_relevant_segment(user_query, segments):
64
 
65
  def generate_response(user_query, relevant_segment):
66
  """
67
- Generate a response emphasizing the bot's capability in providing chess information.
68
  """
69
  try:
70
- user_message = f"Here's the information on chess: {relevant_segment}"
71
 
72
  # Append user's message to messages list
73
  messages.append({"role": "user", "content": user_message})
74
 
 
75
  response = openai.ChatCompletion.create(
76
  model="gpt-3.5-turbo",
77
  messages=messages,
@@ -94,49 +95,72 @@ def generate_response(user_query, relevant_segment):
94
  print(f"Error in generating response: {e}")
95
  return f"Error in generating response: {e}"
96
 
97
- def query_model(question):
98
  """
99
- Process a question, find relevant information, and generate a response.
100
  """
101
- if question == "":
102
- return "Welcome to ChessBot! Ask me anything about chess rules, strategies, and terminology."
103
- relevant_segment = find_relevant_segment(question, segments)
104
- if not relevant_segment:
105
- return "Could not find specific information. Please refine your question."
106
- response = generate_response(question, relevant_segment)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  return response
108
 
109
  # Define the welcome message and specific topics the chatbot can provide information about
110
  welcome_message = """
111
- # ♟️ Welcome to Song Seeker!
112
-
113
- ## Your AI-driven assistant for music curation. Created by Fenet, Lia, and Zamira of the 2024 Kode With Klossy DC Camp.
114
  """
115
 
116
  topics = """
117
- ### Feel Free to ask me anything from the topics below!
 
118
  - Sad songs
119
- - Happy songs
120
- - Game phases
121
- - Common strategies
122
- - Chess terminology
123
- - Famous games
124
- - Chess tactics
125
  """
126
 
127
  # Setup the Gradio Blocks interface with custom layout components
128
- with gr.Blocks(theme='JohnSmith9982/small_and_pretty') as demo:
129
  gr.Markdown(welcome_message) # Display the formatted welcome message
130
  with gr.Row():
131
  with gr.Column():
132
  gr.Markdown(topics) # Show the topics on the left side
133
  with gr.Row():
134
  with gr.Column():
135
- question = gr.Textbox(label="Your question", placeholder="What do you want to ask about?")
136
- answer = gr.Textbox(label="ChessBot Response", placeholder="ChessBot will respond here...", interactive=False, lines=10)
137
  submit_button = gr.Button("Submit")
138
  submit_button.click(fn=query_model, inputs=question, outputs=answer)
139
-
140
 
141
  # Launch the Gradio app to allow user interaction
142
  demo.launch(share=True)
 
 
6
  os.environ["TOKENIZERS_PARALLELISM"] = "false"
7
 
8
  # Initialize paths and model identifiers for easy configuration and maintenance
9
+ filename = "output_topic_details.txt" # Path to the file storing song-specific details
10
  retrieval_model_name = 'output/sentence-transformer-finetuned/'
11
 
12
  openai.api_key = os.environ["OPENAI_API_KEY"]
13
 
14
+ system_message = "You are a song chatbot specialized in providing song recommendations based on mood."
15
  # Initial system message to set the behavior of the assistant
16
  messages = [{"role": "system", "content": system_message}]
17
 
 
64
 
65
  def generate_response(user_query, relevant_segment):
66
  """
67
+ Generate a response emphasizing the bot's capability in providing song recommendations.
68
  """
69
  try:
70
+ user_message = f"Here's the information on songs: {relevant_segment}"
71
 
72
  # Append user's message to messages list
73
  messages.append({"role": "user", "content": user_message})
74
 
75
+ # Use OpenAI's API to generate a response based on the user's query and system messages
76
  response = openai.ChatCompletion.create(
77
  model="gpt-3.5-turbo",
78
  messages=messages,
 
95
  print(f"Error in generating response: {e}")
96
  return f"Error in generating response: {e}"
97
 
98
+ def recommend_songs_based_on_mood(mood):
99
  """
100
+ Recommend songs based on the user's mood query.
101
  """
102
+ # Example logic to recommend songs based on mood (replace with your actual logic)
103
+ recommended_songs = [
104
+ "Song A",
105
+ "Song B",
106
+ "Song C",
107
+ "Song D",
108
+ "Song E"
109
+ ]
110
+
111
+ # Format the recommendation list as a string
112
+ recommended_songs_str = "\n- " + "\n- ".join(recommended_songs)
113
+
114
+ return f"Here are some songs you might like based on '{mood}' mood:{recommended_songs_str}"
115
+
116
+ def query_model(user_query):
117
+ """
118
+ Process a user's query, find relevant information, and generate a response.
119
+ """
120
+ if user_query == "":
121
+ return "Welcome to SongBot! Ask me for song recommendations based on mood."
122
+
123
+ # Example logic to identify if the user query is related to song recommendations based on mood
124
+ if "recommend" in user_query.lower() and ("song" in user_query.lower() or "music" in user_query.lower()):
125
+ mood = user_query.lower().split("recommend", 1)[1].strip() # Extract mood from query
126
+ response = recommend_songs_based_on_mood(mood)
127
+ else:
128
+ relevant_segment = find_relevant_segment(user_query, segments)
129
+ if not relevant_segment:
130
+ response = "Could not find specific information. Please refine your question."
131
+ else:
132
+ response = generate_response(user_query, relevant_segment)
133
+
134
  return response
135
 
136
  # Define the welcome message and specific topics the chatbot can provide information about
137
  welcome_message = """
138
+ # 🎵 Welcome to Song Seeker!
139
+ ## Your AI-driven assistant for music curation. Created by Fenet, Lia, and Zamira of the 2024 Kode With Klossy DC Camp.
 
140
  """
141
 
142
  topics = """
143
+ ### Feel free to ask me for song recommendations based on mood!
144
+ - Happy songs
145
  - Sad songs
146
+ - Chill songs
147
+ - Angry songs
148
+ - Workout songs
 
 
 
149
  """
150
 
151
  # Setup the Gradio Blocks interface with custom layout components
152
+ with gr.Blocks(theme='dabble') as demo:
153
  gr.Markdown(welcome_message) # Display the formatted welcome message
154
  with gr.Row():
155
  with gr.Column():
156
  gr.Markdown(topics) # Show the topics on the left side
157
  with gr.Row():
158
  with gr.Column():
159
+ question = gr.Textbox(label="Your mood (e.g., happy, sad)", placeholder="What mood are you in?")
160
+ answer = gr.Textbox(label="SongBot Response", placeholder="SongBot will respond here...", interactive=False, lines=10)
161
  submit_button = gr.Button("Submit")
162
  submit_button.click(fn=query_model, inputs=question, outputs=answer)
 
163
 
164
  # Launch the Gradio app to allow user interaction
165
  demo.launch(share=True)
166
+