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