Spaces:
Sleeping
Sleeping
diyapaudyal
commited on
Commit
•
ed00df1
1
Parent(s):
9c1459b
Update app.py
Browse files
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
|
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
|
15 |
# Initial system message to set the behavior of the assistant
|
16 |
messages = [{"role": "system", "content": system_message}]
|
17 |
|
@@ -45,17 +45,17 @@ def find_relevant_segment(user_query, segments):
|
|
45 |
try:
|
46 |
# Lowercase the query for better matching
|
47 |
lower_query = user_query.lower()
|
48 |
-
|
49 |
# Encode the query and the segments
|
50 |
query_embedding = retrieval_model.encode(lower_query)
|
51 |
segment_embeddings = retrieval_model.encode(segments)
|
52 |
-
|
53 |
# Compute cosine similarities between the query and the segments
|
54 |
similarities = util.pytorch_cos_sim(query_embedding, segment_embeddings)[0]
|
55 |
-
|
56 |
# Find the index of the most similar segment
|
57 |
best_idx = similarities.argmax()
|
58 |
-
|
59 |
# Return the most relevant segment
|
60 |
return segments[best_idx]
|
61 |
except Exception as e:
|
@@ -64,14 +64,14 @@ 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
|
68 |
"""
|
69 |
try:
|
70 |
-
user_message = f"Here's the information on
|
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,
|
@@ -81,15 +81,15 @@ def generate_response(user_query, relevant_segment):
|
|
81 |
frequency_penalty=0,
|
82 |
presence_penalty=0
|
83 |
)
|
84 |
-
|
85 |
# Extract the response text
|
86 |
output_text = response['choices'][0]['message']['content'].strip()
|
87 |
-
|
88 |
# Append assistant's message to messages list for context
|
89 |
messages.append({"role": "assistant", "content": output_text})
|
90 |
-
|
91 |
return output_text
|
92 |
-
|
93 |
except Exception as e:
|
94 |
print(f"Error in generating response: {e}")
|
95 |
return f"Error in generating response: {e}"
|
@@ -99,31 +99,30 @@ def query_model(question):
|
|
99 |
Process a question, find relevant information, and generate a response.
|
100 |
"""
|
101 |
if question == "":
|
102 |
-
return "Welcome to
|
103 |
relevant_segment = find_relevant_segment(question, segments)
|
104 |
if not relevant_segment:
|
105 |
-
return "Could not find specific information. Please
|
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 |
-
#
|
112 |
|
113 |
-
##
|
114 |
"""
|
115 |
|
116 |
topics = """
|
117 |
-
### Feel Free to
|
118 |
-
-
|
119 |
-
-
|
120 |
-
-
|
121 |
-
-
|
122 |
-
-
|
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
|
@@ -133,10 +132,10 @@ with gr.Blocks(theme='JohnSmith9982/small_and_pretty') as demo:
|
|
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="
|
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 skincare-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 skincare chatbot specialized in providing information on skincare dupes, ingredients , and effects."
|
15 |
# Initial system message to set the behavior of the assistant
|
16 |
messages = [{"role": "system", "content": system_message}]
|
17 |
|
|
|
45 |
try:
|
46 |
# Lowercase the query for better matching
|
47 |
lower_query = user_query.lower()
|
48 |
+
|
49 |
# Encode the query and the segments
|
50 |
query_embedding = retrieval_model.encode(lower_query)
|
51 |
segment_embeddings = retrieval_model.encode(segments)
|
52 |
+
|
53 |
# Compute cosine similarities between the query and the segments
|
54 |
similarities = util.pytorch_cos_sim(query_embedding, segment_embeddings)[0]
|
55 |
+
|
56 |
# Find the index of the most similar segment
|
57 |
best_idx = similarities.argmax()
|
58 |
+
|
59 |
# Return the most relevant segment
|
60 |
return segments[best_idx]
|
61 |
except Exception as e:
|
|
|
64 |
|
65 |
def generate_response(user_query, relevant_segment):
|
66 |
"""
|
67 |
+
Generate a response emphasizing the bot's capability in providing skincare information.
|
68 |
"""
|
69 |
try:
|
70 |
+
user_message = f"Here's the information on skincare: {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,
|
|
|
81 |
frequency_penalty=0,
|
82 |
presence_penalty=0
|
83 |
)
|
84 |
+
|
85 |
# Extract the response text
|
86 |
output_text = response['choices'][0]['message']['content'].strip()
|
87 |
+
|
88 |
# Append assistant's message to messages list for context
|
89 |
messages.append({"role": "assistant", "content": output_text})
|
90 |
+
|
91 |
return output_text
|
92 |
+
|
93 |
except Exception as e:
|
94 |
print(f"Error in generating response: {e}")
|
95 |
return f"Error in generating response: {e}"
|
|
|
99 |
Process a question, find relevant information, and generate a response.
|
100 |
"""
|
101 |
if question == "":
|
102 |
+
return "Welcome to DupeBot! Ask me anything about skincare dupes and products for your skintype."
|
103 |
relevant_segment = find_relevant_segment(question, segments)
|
104 |
if not relevant_segment:
|
105 |
+
return "Could not find specific information. Please consult a dermatologist instead."
|
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 DupeBot! 🌸🧴
|
112 |
|
113 |
+
## DupeBot is your personal assistant for all skin-related queries. Created by SCHOLAR1, SCHOLAR2, and SCHOLAR3 of the 2024 Kode With Klossy CITY Camp.
|
114 |
"""
|
115 |
|
116 |
topics = """
|
117 |
+
### Feel Free to Ask Me about Any of the Following Topics:
|
118 |
+
- Skincare Dupes
|
119 |
+
- Makeup dupes
|
120 |
+
- Ideal Skincare Ingredients for Your Skin Type
|
121 |
+
- Products Targeted Towards Your Specific Skin Issues
|
122 |
+
- Uses for Various Ingredients
|
|
|
|
|
123 |
"""
|
124 |
|
125 |
+
|
126 |
# Setup the Gradio Blocks interface with custom layout components
|
127 |
with gr.Blocks(theme='JohnSmith9982/small_and_pretty') as demo:
|
128 |
gr.Markdown(welcome_message) # Display the formatted welcome message
|
|
|
132 |
with gr.Row():
|
133 |
with gr.Column():
|
134 |
question = gr.Textbox(label="Your question", placeholder="What do you want to ask about?")
|
135 |
+
answer = gr.Textbox(label="DupeBot Response", placeholder="DupeBot will respond here...", interactive=False, lines=10)
|
136 |
submit_button = gr.Button("Submit")
|
137 |
submit_button.click(fn=query_model, inputs=question, outputs=answer)
|
138 |
+
|
139 |
|
140 |
# Launch the Gradio app to allow user interaction
|
141 |
demo.launch(share=True)
|