arjunakurian commited on
Commit
30717ee
1 Parent(s): 31b84bf

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ from flask import Flask, render_template, request, redirect, url_for
3
+ import openai
4
+
5
+ # Set up OpenAI API key
6
+ openai.api_key = "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"
7
+
8
+ # Load dataset from csv file
9
+ dataset = pd.read_csv("dataset.csv")
10
+
11
+ # Define function to prompt user and get response
12
+ def ask_question(prompt):
13
+ response = input(prompt + " ")
14
+ return response.lower()
15
+
16
+ # Define function to generate advice based on user input
17
+ def generate_advice(problem):
18
+ # Construct prompt for OpenAI API
19
+ prompt = f"I am feeling {problem}. Please provide some advice."
20
+ # Use OpenAI's GPT-3 model to generate advice based on the prompt
21
+ response = openai.Completion.create(
22
+ engine="text-davinci-002",
23
+ prompt=prompt,
24
+ max_tokens=1024,
25
+ n=1,
26
+ stop=None,
27
+ temperature=0.7
28
+ )
29
+ # Extract the generated advice from the API response
30
+ advice = response.choices[0].text.strip()
31
+ return advice
32
+
33
+ app = Flask(__name__)
34
+
35
+ @app.route('/')
36
+ def index():
37
+ return render_template('index.html')
38
+
39
+ @app.route('/advice', methods=['POST'])
40
+ def advice():
41
+ age = int(request.form['age'])
42
+ return redirect(url_for('show_questions', age=age))
43
+
44
+ @app.route('/questions/<age>', methods=['GET', 'POST'])
45
+ def show_questions(age):
46
+ age = int(age) # Convert age to integer
47
+
48
+ if request.method == 'GET':
49
+ if age >= 13 and age <= 17:
50
+ questions = dataset["Questions for 13-17"].tolist()
51
+ elif age >= 18 and age <= 21:
52
+ questions = dataset["Questions for 18-21"].tolist()
53
+ elif age >= 21 and age <= 25:
54
+ questions = dataset["Questions for 21-25"].tolist()
55
+ elif age >= 25 and age <= 40:
56
+ questions = dataset["Questions for 25-40"].tolist()
57
+ elif age >= 40 and age <= 60:
58
+ questions = dataset["Questions for 40-60"].tolist()
59
+ else:
60
+ questions = dataset["Questions for 60+"].tolist()
61
+
62
+ return render_template('questions.html', age=age, questions=questions)
63
+
64
+ elif request.method == 'POST':
65
+ answers = []
66
+ for i in range(6):
67
+ answer = request.form.get(f'answer{i+1}')
68
+ answers.append(answer)
69
+ # Process the answers and generate advice
70
+ advice = generate_advice(answers)
71
+ return render_template('advice.html', advice=advice)
72
+
73
+ if __name__ == '__main__':
74
+ app.run(debug=True)