File size: 3,297 Bytes
9fc507e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import openai
from dotenv import load_dotenv
import os
from flask import Flask,jsonify,request

load_dotenv()
openai.api_key=os.getenv("OPENAI_API_KEY")
def sentiment_analysis(transcription):
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        temperature=0,
        messages=[
            {
                "role": "system",
                "content": "As an AI with expertise in language and emotion analysis, your task is to analyze the sentiment of the following text. Please consider the overall tone of the discussion, the emotion conveyed by the language used, and the context in which words and phrases are used. Indicate whether the sentiment is generally positive, negative, or neutral, and provide brief explanations for your analysis where possible. just say in one word"
            },
            {
                "role": "user",
                "content": transcription
            }
        ]
    )
    return response['choices'][0]['message']['content']

def positive_summary_extraction(transcription):
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        temperature=0,
        messages=[
            {
                "role": "system",
                "content": "The following is information from a customer on his/her experience with our Anytime Fitness gym branch. Please create a 2-3 sentence review based on this info that is cheerful and reflects well on the business include emojis"
            },
            {
                "role": "user",
                "content": transcription
            }
        ]
    )
    return response['choices'][0]['message']['content']


def nagetive_summary_extraction(transcription):
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        temperature=0,
        messages=[
            {
                "role": "system",
                "content": "The following is information for feedback from a customer on his/her negative experience with our Anytime Fitness gym branch. Please create a 2-3 sentence summary based on this info that is objective and to the point so the gym understands what to do next"
            },
            {
                "role": "user",
                "content": transcription
            }
        ]
    )
    return response['choices'][0]['message']['content']

app=Flask(__name__)


@app.route('/')
def index():
  return "Hello World"

@app.route('/sentiment')
def getSentiment():
    query=request.args.get('query','')
    if query.strip()=='':
        return "Provide Valid Question"
    return jsonify({"question" : query,
            "answer" : sentiment_analysis(query)
            })

@app.route('/summarize')
def getSummary():
    query_type=request.args.get('type','positive')
    query=request.args.get('query','')
    if query.strip()=='':
        return "Provide Valid Question"
    elif query_type.lower()=='positive':
        return jsonify({"question" : query,
                "answer" : positive_summary_extraction(query)
                })
    elif query_type.lower()=='negative':
        return jsonify({"question" : query,
                "answer" : nagetive_summary_extraction(query)
                })
    return "Provide Valid Question"

if __name__=="__main__":
    app.run(debug=True)