forked from sumodm/hackathon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
71 lines (56 loc) · 2.18 KB
/
main.py
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
import os
import json
from jinja2 import Template, Environment, FileSystemLoader
import openai as oai
from flask import Flask, jsonify, request
app = Flask(__name__)
env = Environment(loader=FileSystemLoader("prompts/"))
env.lstrip_blocks = True
env.trim_blocks = True
oai.api_key = os.getenv("OPENAI_API_KEY")
with open("concepts.json", 'r') as f:
concepts = json.load(f)
with open("data/topic_graph.json", 'r') as f:
data = json.load(f)
with open("phy_subtop_qns.json", 'r') as f:
phy_subtop_qns = json.load(f)
mapping = {"Physics":0, "Chemistry":1, "Maths":2, "Biology":3}
def call_openai(prompt, max_tokens=1000, temp=0.3):
completion = oai.ChatCompletion.create(model="gpt-3.5-turbo", messages = [{"role":"user", "content":prompt}],
temperature = temp, top_p = 1, n=1, max_tokens=max_tokens)
return(completion.choices[0].message.content)
@app.route('/get_subtopic_qns')
def get_subtopic_qns():
board = request.args.get('board')
grade = request.args.get('grade')
subject = request.args.get('subject')
topics = request.args.get('topic')
topic_ids = request.args.get('topic_id')
if subject != 'Physics':
return {'None'}
results = {k:v for k,v in phy_subtop_qns.items() if k in topic_ids}
return jsonify(results)
@app.route('/get_subtopic_qn')
def get_subtopic_qn():
board = request.args.get('board')
grade = request.args.get('grade')
subject = request.args.get('subject')
topic = request.args.get('topic')
topic_id = request.args.get('topic_id')
subtopic0_tmp = "subtopics0.txt"
print(mapping[subject])
print(topic_id)
subtopics = data[int(grade)-5]["subjects"][mapping[subject]]["topics"][int(topic_id)]["sub_topics"]
print(subtopics)
prompt0_tmpl = env.get_template(subtopic0_tmp)
prompt0 = prompt0_tmpl.render(subject=subject, grade=grade, board=board,
topic=topic, subtopics = subtopics,
concepts = concepts[topic])
print(prompt0)
question = call_openai(prompt0)
return jsonify({'status':'in-progress', 'type': 'question', 'question': question})
@app.route('/ping')
def ping():
return 'Pong!'
if __name__ == '__main__':
app.run()