Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -66,68 +66,6 @@ def generate_response(user_query, relevant_segment):
|
|
66 |
Generate a response emphasizing the bot's capability to provide information related to composting food.
|
67 |
"""
|
68 |
|
69 |
-
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
70 |
-
|
71 |
-
try:
|
72 |
-
# Define the messages
|
73 |
-
system_message = "You are a chatbot specialized in providing information about food composting tips, tricks, and basics."
|
74 |
-
user_message = f"Here's the information on composting: {relevant_segment} {user_query}"
|
75 |
-
messages = [
|
76 |
-
{"role": "system", "content": system_message},
|
77 |
-
{"role": "user", "content": user_message}
|
78 |
-
]
|
79 |
-
|
80 |
-
# Generate the response using gpt-3.5-turbo-instruct
|
81 |
-
response = openai.ChatCompletion.create(
|
82 |
-
model="gpt-4o",
|
83 |
-
messages=messages,
|
84 |
-
max_tokens=300,
|
85 |
-
temperature=0.5,
|
86 |
-
top_p=1
|
87 |
-
frequency_penalty=0.5,
|
88 |
-
presence_penalty=0.5
|
89 |
-
)
|
90 |
-
|
91 |
-
# Extract the generated text
|
92 |
-
generated_text = response['choices'][0]['message']['content'].strip()
|
93 |
-
|
94 |
-
return generated_text
|
95 |
-
|
96 |
-
except Exception as e:
|
97 |
-
print(f"Error in generating response: {e}")
|
98 |
-
return f"Error in generating response: {e}"
|
99 |
-
|
100 |
-
|
101 |
-
# Function to classify the generated response and get confidence scores
|
102 |
-
def classify_response(response_text, candidate_labels=["food composting", "other"]):
|
103 |
-
try:
|
104 |
-
# Perform classification
|
105 |
-
response = classifier(
|
106 |
-
sequences=response_text,
|
107 |
-
candidate_labels=candidate_labels
|
108 |
-
)
|
109 |
-
|
110 |
-
# Extract the confidence score for the most likely label
|
111 |
-
confidence_score = response['scores'][0] # Confidence score for the most likely label
|
112 |
-
|
113 |
-
return confidence_score
|
114 |
-
|
115 |
-
except Exception as e:
|
116 |
-
print(f"Error in classifying response: {e}")
|
117 |
-
return f"Error in classifying response: {e}"
|
118 |
-
|
119 |
-
# Example usage
|
120 |
-
openai.api_key = 'sk-proj-X437DVJaksFxSGcGBcuzT3BlbkFJBcQkP2utlrZg09UMRIIZ'
|
121 |
-
user_query = "How to compost food scraps?"
|
122 |
-
relevant_segment = "Food composting involves..."
|
123 |
-
generated_response = generate_response(user_query, relevant_segment)
|
124 |
-
|
125 |
-
if generated_response:
|
126 |
-
confidence_score = classify_response(generated_response)
|
127 |
-
print(f"Generated Response: {generated_response}")
|
128 |
-
print(f"Confidence Score: {confidence_score}")
|
129 |
-
|
130 |
-
'''
|
131 |
try:
|
132 |
system_message = "You are a chatbot specialized in providing information about food composting tips, tricks, and basics."
|
133 |
user_message = f"Here's the information on composting: {relevant_segment}"
|
@@ -148,7 +86,6 @@ if generated_response:
|
|
148 |
except Exception as e:
|
149 |
print(f"Error in generating response: {e}")
|
150 |
return f"Error in generating response: {e}"
|
151 |
-
'''
|
152 |
|
153 |
|
154 |
def query_model(question):
|
|
|
66 |
Generate a response emphasizing the bot's capability to provide information related to composting food.
|
67 |
"""
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
try:
|
70 |
system_message = "You are a chatbot specialized in providing information about food composting tips, tricks, and basics."
|
71 |
user_message = f"Here's the information on composting: {relevant_segment}"
|
|
|
86 |
except Exception as e:
|
87 |
print(f"Error in generating response: {e}")
|
88 |
return f"Error in generating response: {e}"
|
|
|
89 |
|
90 |
|
91 |
def query_model(question):
|