import openai
import re
openai.api_key = "<your_api_key>"
def get_sentiment(input_text):
prompt = f"Respond in the json format: {{'response': sentiment_classification}}\nText: {input_text}\nSentiment (positive, neutral, negative):"
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": prompt}
],
max_tokens=40,
n=1,
stop=None,
temperature=0.5,
)
response_text = response.choices[0].message['content'].strip()
sentiment = re.search("negative|neutral|positive", response_text).group(0)
# Добавляем обратно input_text для результата
return {"text": input_text, "response": sentiment}
# Тестовый пример
sample_text = "I had a terrible time at the party last night!"
sentiment = get_sentiment(sample_text)
print("Result\n",f"{sentiment}")
Result:
{'text': 'I had a terrible time at the party last night!', 'response': 'negative'}
import json
input_file_path = "input_texts.txt"
output_file_path = "output_responses.json"
with open(input_file_path, "r") as input_file, open(output_file_path, "w") as output_file:
examples = []
for line in input_file:
text = line.strip()
if text:
examples.append(convert_ls_format(get_sentiment(text)))
output_file.write(json.dumps(examples))
pip install label-studio
label-studio
<View>
<Header value="Choose text sentiment:"/>
<Text name="my_text" value="$reviewText"/>
<Choices name="sentiment" toName="my_text" choice="single" showInline="true">
<Choice value="Positive"/>
<Choice value="Negative"/>
<Choice value="Neutral"/>
</Choices>
</View>
{'summary': 'Artificial intelligence is a rapidly developing technology that can learn from data and make decisions without being explicitly programmed. It has the potential to help solve some of the world's most pressing problems, but there are concerns about risks and ethical considerations such as bias and job displacement. It is important to consider AI's impact and ensure responsible and beneficial use.'}
{'diagnosis': 'Pneumonia'}
{
"data": [
{
"word": "Harry",
"entity": "B-PER"
},
{
"word": "Potter",
"entity": "I-PER"
},
{
"word": "was",
"entity": "O"
},
{
"word": "a",
"entity": "O"
},
{
"word": "student",
"entity": "O"
},
{
"word": "at",
"entity": "O"
},
{
"word": "Hogwarts",
"entity": "B-LOC"
}
]
}