studfaceval / app5_selectbox /df4_sentiment_analysis.py
MENG21's picture
Upload 68 files
e4fe207 verified
import g4f
import time
def sentiment_func(message_list):
message_list=[
"Your lectures were so dull and uninspiring, I couldn't help but zone out",
"you have an extraordinary talent for leadership.",
"The instructor's indifference made it difficult to remain engaged or motivated",
"The lack of enthusiasm from the instructor made the class feel like a chore",
"Salamat sa iyong inspirasyon at dedikasyon sa aming edukasyon.",
"Sa bawat pagkakataon, lumalalim ang aming pag-unawa sa mga aralin.",
"Thanks for being dedicated to our education.",
"You show the societal importance of education.",
"The instructor's disinterested demeanor was reflected in the overall class atmosphere"
]
message_list = '[label]\n'.join(message_list)
# print(message_list)
prompt = f"""
Please provide a single-word response per sentence.
label the following sentences if it is positive,negative
sentence list = {message_list}
your output is should in comma separated
example output : positive,negative,negative,positive
"""
# Please provide a single-word response.
print(prompt)
while True:
try:
# streamed completion
response = g4f.ChatCompletion.create(
model="gpt-3.5-turbo",
# provider=g4f.Provider.GeekGpt,
provider=g4f.Provider.You,
# model="gpt-4",
# provider=g4f.Provider.Bing,
messages=[{"role": "user", "content": prompt}],
stream=True,
)
returned_output = ""
for message in response:
# print(message, flush=True, end='')
returned_output += message
# print(message)
returned_output = returned_output.split(',')
# Trim extra white spaces and convert to lowercase
returned_output = [item.strip().lower() for item in returned_output]
return returned_output
# print(returned_output)
break # Exit the loop if the chat completes successfully
except Exception as e:
# Handle the error (e.g., log it or take appropriate action)
# Sleep for a moment before retrying
print("error....",e)
time.sleep(0.4)