Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,11 +8,11 @@ import torch
|
|
| 8 |
|
| 9 |
with open("charities.txt", "r", encoding="utf-8") as file:
|
| 10 |
# Read the entire contents of the file and store it in a variable
|
| 11 |
-
|
| 12 |
|
| 13 |
-
|
| 14 |
# Read the entire contents of the file and store it in a variable
|
| 15 |
-
|
| 16 |
|
| 17 |
#with open("time_management.txt", "r", encoding="utf-8") as file:
|
| 18 |
# Read the entire contents of the file and store it in a variable
|
|
@@ -27,7 +27,7 @@ with open("charities.txt", "r", encoding="utf-8") as file:
|
|
| 27 |
# financial_aid = file.read()
|
| 28 |
|
| 29 |
# Print the text below
|
| 30 |
-
print(
|
| 31 |
|
| 32 |
# ===== APPLY THE COMPLETE WORKFLOW =====
|
| 33 |
|
|
@@ -117,16 +117,16 @@ def get_top_chunks(query, chunk_embeddings, text_chunks):
|
|
| 117 |
|
| 118 |
# Print the top results
|
| 119 |
#print(top_results)
|
| 120 |
-
cleaned_chunks = preprocess_text(
|
| 121 |
-
cleaned_chunks2= preprocess_text(
|
| 122 |
-
cleaned_chunks3= preprocess_text(time_management)
|
| 123 |
-
cleaned_chunks4= preprocess_text(financial_aid)
|
| 124 |
|
| 125 |
|
| 126 |
chunk_embeddings = create_embeddings(cleaned_chunks)
|
| 127 |
chunk_embeddings2 = create_embeddings(cleaned_chunks2)
|
| 128 |
-
chunk_embeddings3 = create_embeddings(cleaned_chunks3)
|
| 129 |
-
chunk_embeddings4 = create_embeddings(cleaned_chunks4)
|
| 130 |
|
| 131 |
|
| 132 |
#AI API being used
|
|
@@ -136,38 +136,38 @@ client= InferenceClient("openai/gpt-oss-20b")
|
|
| 136 |
information=""
|
| 137 |
def respond(message,history):
|
| 138 |
topic_chunks=[]
|
| 139 |
-
if chatbot_topic=="
|
| 140 |
|
| 141 |
topic_chunks=get_top_chunks(message, chunk_embeddings, cleaned_chunks)
|
| 142 |
print(topic_chunks)
|
| 143 |
|
| 144 |
-
elif chatbot_topic=="
|
| 145 |
topic_chunks=get_top_chunks(message, chunk_embeddings2, cleaned_chunks2)
|
| 146 |
print(topic_chunks)
|
| 147 |
|
| 148 |
|
| 149 |
-
elif chatbot_topic=="Time Management":
|
| 150 |
-
|
| 151 |
-
|
| 152 |
|
| 153 |
|
| 154 |
-
elif chatbot_topic=="Financial Aid":
|
| 155 |
-
|
| 156 |
-
|
| 157 |
|
| 158 |
#return information
|
| 159 |
#return topic_chunks
|
| 160 |
-
|
| 161 |
-
|
| 162 |
|
| 163 |
-
|
| 164 |
-
|
| 165 |
|
| 166 |
-
if chatbot_mode=="Parent Mode":
|
| 167 |
-
|
| 168 |
|
| 169 |
-
else:
|
| 170 |
-
|
| 171 |
|
| 172 |
if history:
|
| 173 |
messages.extend(history) #keep adding history
|
|
@@ -180,14 +180,14 @@ def respond(message,history):
|
|
| 180 |
|
| 181 |
### STEP 6
|
| 182 |
# Call the preprocess_text function and store the result in a cleaned_chunks variable
|
| 183 |
-
cleaned_chunks = preprocess_text(
|
| 184 |
-
top_results = get_top_chunks("
|
| 185 |
|
| 186 |
print(top_results)
|
| 187 |
|
| 188 |
#Defining chatbot giving user a UI to interact, see their conversation history, and see new messages using built in gr feature
|
| 189 |
#ChatInterface requires at least one parameter(a function)
|
| 190 |
-
chatbot = gr.ChatInterface(respond,type="messages", title="
|
| 191 |
|
| 192 |
def save_chat_history(history, username):
|
| 193 |
if not username:
|
|
@@ -234,9 +234,9 @@ with gr.Blocks(
|
|
| 234 |
)
|
| 235 |
) as demo:
|
| 236 |
with gr.Row(scale=1):
|
| 237 |
-
chatbot_topic=gr.CheckboxGroup(["
|
| 238 |
with gr.Row(scale=1):
|
| 239 |
-
chatbot_mode=gr.CheckboxGroup(["
|
| 240 |
|
| 241 |
#with gr.Row():
|
| 242 |
#save_button = gr.Button("💾 Save Chat History",
|
|
@@ -248,8 +248,8 @@ with gr.Blocks(
|
|
| 248 |
|
| 249 |
gr.ChatInterface(
|
| 250 |
fn=respond,
|
| 251 |
-
title="
|
| 252 |
-
description="Ask about your
|
| 253 |
type="messages",
|
| 254 |
|
| 255 |
|
|
@@ -272,24 +272,24 @@ with gr.Blocks(
|
|
| 272 |
|
| 273 |
#with gr.Blocks() as demo:
|
| 274 |
#chatbot = gr.Chatbot()
|
| 275 |
-
username_input = gr.Textbox(label="Username")
|
| 276 |
-
save_button = gr.Button("Save Chat History")
|
| 277 |
-
download_button = gr.File(label="Download Chat History", visible=False)
|
| 278 |
|
| 279 |
-
save_button.click(
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
).then(
|
| 284 |
-
|
| 285 |
-
|
| 286 |
)
|
| 287 |
-
save_button = gr.Button("💾 Save Chat History",
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
download_button = gr.File(interactive=True,
|
| 291 |
-
|
| 292 |
-
|
| 293 |
|
| 294 |
#launching chatbot
|
| 295 |
demo.launch()
|
|
|
|
| 8 |
|
| 9 |
with open("charities.txt", "r", encoding="utf-8") as file:
|
| 10 |
# Read the entire contents of the file and store it in a variable
|
| 11 |
+
charities_text = file.read()
|
| 12 |
|
| 13 |
+
with open("financial_advice.txt", "r", encoding="utf-8") as file:
|
| 14 |
# Read the entire contents of the file and store it in a variable
|
| 15 |
+
financial_advice_text = file.read()
|
| 16 |
|
| 17 |
#with open("time_management.txt", "r", encoding="utf-8") as file:
|
| 18 |
# Read the entire contents of the file and store it in a variable
|
|
|
|
| 27 |
# financial_aid = file.read()
|
| 28 |
|
| 29 |
# Print the text below
|
| 30 |
+
print(charities_text)
|
| 31 |
|
| 32 |
# ===== APPLY THE COMPLETE WORKFLOW =====
|
| 33 |
|
|
|
|
| 117 |
|
| 118 |
# Print the top results
|
| 119 |
#print(top_results)
|
| 120 |
+
cleaned_chunks = preprocess_text(charities_text)
|
| 121 |
+
cleaned_chunks2= preprocess_text(financial_advice_text)
|
| 122 |
+
#cleaned_chunks3= preprocess_text(time_management)
|
| 123 |
+
#cleaned_chunks4= preprocess_text(financial_aid)
|
| 124 |
|
| 125 |
|
| 126 |
chunk_embeddings = create_embeddings(cleaned_chunks)
|
| 127 |
chunk_embeddings2 = create_embeddings(cleaned_chunks2)
|
| 128 |
+
#chunk_embeddings3 = create_embeddings(cleaned_chunks3)
|
| 129 |
+
#chunk_embeddings4 = create_embeddings(cleaned_chunks4)
|
| 130 |
|
| 131 |
|
| 132 |
#AI API being used
|
|
|
|
| 136 |
information=""
|
| 137 |
def respond(message,history):
|
| 138 |
topic_chunks=[]
|
| 139 |
+
if chatbot_topic=="Helping Charities":
|
| 140 |
|
| 141 |
topic_chunks=get_top_chunks(message, chunk_embeddings, cleaned_chunks)
|
| 142 |
print(topic_chunks)
|
| 143 |
|
| 144 |
+
elif chatbot_topic=="Financial Aid":
|
| 145 |
topic_chunks=get_top_chunks(message, chunk_embeddings2, cleaned_chunks2)
|
| 146 |
print(topic_chunks)
|
| 147 |
|
| 148 |
|
| 149 |
+
#elif chatbot_topic=="Time Management":
|
| 150 |
+
# topic_chunks=get_top_chunks(message, chunk_embeddings3, cleaned_chunks3)
|
| 151 |
+
# print(topic_chunks)
|
| 152 |
|
| 153 |
|
| 154 |
+
#elif chatbot_topic=="Financial Aid":
|
| 155 |
+
# topic_chunks=get_top_chunks(message, chunk_embeddings4, cleaned_chunks4)
|
| 156 |
+
# print(topic_chunks)
|
| 157 |
|
| 158 |
#return information
|
| 159 |
#return topic_chunks
|
| 160 |
+
chatbot_mode=="Advice Mode"
|
| 161 |
+
messages = [{"role": "assistant", "content": f"You are a helpful and insightful chatbot who acts like a financial advisor of a university student who wants to learn to manage their personal finances. You analyse their situation and give relevant advice and insights. You only answer in complete sentences with correct grammar, punctuation, and complete ideas. You respond clearly in under five complete bullet points under 500 characters. When you give advice, keep in mind the following information {topic_chunks}"}]
|
| 162 |
|
| 163 |
+
# if chatbot_mode=="Guidance Counselor Mode":
|
| 164 |
+
# messages = [{"role": "assistant", "content": f"You act as a helpful guidance counselor with an educated understanding of high school life and college admissions. You guide the student to consider their academic potential, while maintaining the passion and balance they need. You only answer in complete sentences with correct grammar, punctuation, and complete ideas. When you give advice, keep in mind the following information {topic_chunks}"}]
|
| 165 |
|
| 166 |
+
# if chatbot_mode=="Parent Mode":
|
| 167 |
+
# messages = [{"role": "assistant", "content": f"You are a guiding, nurturing, and protective parent who wants their student to reach their fullest potential while learning to grow up with the proper physical, emotional, and social development. You want to build your student into a responsible adult, but also want them to pursue success in their life and establish a good future. You only answer in complete sentences with correct grammar, punctuation, and complete ideas. When you give advice, keep in mind the following information {information}"}]
|
| 168 |
|
| 169 |
+
#else:
|
| 170 |
+
# messages = [{"role": "assistant", "content": f"You are a friendly, helpful chatbot that gives academic advice to disadvantaged students about their education based on their question. You only answer in complete sentences with correct grammar, punctuation, and complete ideas. When you give advice, keep in mind the following information {topic_chunks}"}]
|
| 171 |
|
| 172 |
if history:
|
| 173 |
messages.extend(history) #keep adding history
|
|
|
|
| 180 |
|
| 181 |
### STEP 6
|
| 182 |
# Call the preprocess_text function and store the result in a cleaned_chunks variable
|
| 183 |
+
cleaned_chunks = preprocess_text(financial_advice_text) # Complete this line
|
| 184 |
+
top_results = get_top_chunks("What financial advice you give me?", chunk_embeddings, cleaned_chunks)
|
| 185 |
|
| 186 |
print(top_results)
|
| 187 |
|
| 188 |
#Defining chatbot giving user a UI to interact, see their conversation history, and see new messages using built in gr feature
|
| 189 |
#ChatInterface requires at least one parameter(a function)
|
| 190 |
+
chatbot = gr.ChatInterface(respond,type="messages", title="Finance Management Hub", theme="Taithrah/Minimal")
|
| 191 |
|
| 192 |
def save_chat_history(history, username):
|
| 193 |
if not username:
|
|
|
|
| 234 |
)
|
| 235 |
) as demo:
|
| 236 |
with gr.Row(scale=1):
|
| 237 |
+
chatbot_topic=gr.CheckboxGroup(["Helping Charities", "Financial Aid"], label="What would you like advice about?")
|
| 238 |
with gr.Row(scale=1):
|
| 239 |
+
chatbot_mode=gr.CheckboxGroup(["Advice Mode"], label="How would you like the chatbot to respond?")
|
| 240 |
|
| 241 |
#with gr.Row():
|
| 242 |
#save_button = gr.Button("💾 Save Chat History",
|
|
|
|
| 248 |
|
| 249 |
gr.ChatInterface(
|
| 250 |
fn=respond,
|
| 251 |
+
title="Finance Management Hub",
|
| 252 |
+
description="Ask about your personal finance",
|
| 253 |
type="messages",
|
| 254 |
|
| 255 |
|
|
|
|
| 272 |
|
| 273 |
#with gr.Blocks() as demo:
|
| 274 |
#chatbot = gr.Chatbot()
|
| 275 |
+
#username_input = gr.Textbox(label="Username")
|
| 276 |
+
#save_button = gr.Button("Save Chat History")
|
| 277 |
+
#download_button = gr.File(label="Download Chat History", visible=False)
|
| 278 |
|
| 279 |
+
#save_button.click(
|
| 280 |
+
# fn=save_chat_history,
|
| 281 |
+
# inputs=[chatbot, username_input],
|
| 282 |
+
# outputs=download_button
|
| 283 |
+
#).then(
|
| 284 |
+
# fn=lambda: gr.update(visible=True),
|
| 285 |
+
# outputs=download_button
|
| 286 |
)
|
| 287 |
+
#save_button = gr.Button("💾 Save Chat History",
|
| 288 |
+
# variant="primary",
|
| 289 |
+
# size="sm")
|
| 290 |
+
#download_button = gr.File(interactive=True,
|
| 291 |
+
# visible=True,
|
| 292 |
+
# elem_classes=["download-btn"])
|
| 293 |
|
| 294 |
#launching chatbot
|
| 295 |
demo.launch()
|