Thillai commited on
Commit
0273fe2
1 Parent(s): c30c415

Create index.py

Browse files
Files changed (1) hide show
  1. index.py +108 -0
index.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import google.generativeai as genai
3
+ import os
4
+ import re
5
+ from flask import Flask, jsonify, request
6
+ from flask_cors import CORS
7
+ import requests
8
+
9
+ app = Flask(__name__)
10
+ CORS(app)
11
+ # Set up the model
12
+ generation_config = {
13
+ "temperature": 0.9,
14
+ "top_p": 1,
15
+ "top_k": 1,
16
+ "max_output_tokens": 2048,
17
+ }
18
+
19
+ safety_settings = [
20
+ {
21
+ "category": "HARM_CATEGORY_HARASSMENT",
22
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE"
23
+ },
24
+ {
25
+ "category": "HARM_CATEGORY_HATE_SPEECH",
26
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE"
27
+ },
28
+ {
29
+ "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
30
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE"
31
+ },
32
+ {
33
+ "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
34
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE"
35
+ },
36
+ ]
37
+
38
+
39
+
40
+
41
+ # model.generate_content("Elon Musk")
42
+ @app.route("/generate", methods=["POST"])
43
+ def generate_func():
44
+ api_key = "AIzaSyCV-2nkHNGMZ-IK84YYdltW2C9tkoUD9so"
45
+ genai.configure(api_key=api_key)
46
+
47
+ model = genai.GenerativeModel(model_name="gemini-1.0-pro",
48
+ generation_config=generation_config,
49
+ safety_settings=safety_settings)
50
+ numberOfQuestions = 5
51
+ selectedOption = "MCQ"
52
+ difficultyLevel = "medium"
53
+ text = "Elon Musk is a business magnate, industrial designer, and engineer. He is the founder, CEO, CTO, and chief designer of SpaceX; early investor, CEO, and product architect of Tesla, Inc.; founder of The Boring Company; co-founder of Neuralink; and co-founder of PayPal. A centibillionaire, Musk is one of the richest people in the world. Musk was born to a Canadian mother and South African father and raised in Pretoria, South Africa. He briefly attended the University of Pretoria before moving to Canada aged 17 to attend Queen's University. He transferred to the University of Pennsylvania two years later, where he received dual bachelor's degrees in economics and physics. He moved to California in 1995 to attend Stanford University but decided instead to pursue a business career, co-founding the web software company Zip2 with his brother Kimbal. The startup was acquired by Compaq for $307 million in 1999. Musk co-founded online bank X.com that same year, which merged with Confinity in 2000 to form the company PayPal and was subsequently bought by eBay in 2002 for $1.5 billion."
54
+ prompt = "Generate {numberOfQuestions} {selectedOption} questions with answer of {difficultyLevel} difficulty based on the context and Generate the output in JSON format which has the field question of type String,options of type list, answer of type String. \nContext: {text}".format(numberOfQuestions=numberOfQuestions, selectedOption=selectedOption, difficultyLevel=difficultyLevel, text=text)
55
+
56
+ response = model.generate_content(prompt)
57
+ formatted_result = re.sub("json", "", response.text)
58
+ res = re.sub("`", "", formatted_result)
59
+
60
+ try :
61
+ data = json.loads(res)
62
+ except Exception as e:
63
+ print(e)
64
+ return jsonify({"error": str(e)})
65
+
66
+ first_key = next(iter(data), None)
67
+
68
+ if first_key == "questions":
69
+ return jsonify(data[first_key])
70
+ else:
71
+ return jsonify(data)
72
+
73
+
74
+ @app.route("/home")
75
+ def read_root():
76
+ return { "hello": "world"}
77
+
78
+
79
+ # @app.route("/items",methods=["POST"])
80
+ # async def read_item():
81
+
82
+ # text = generate()
83
+ # print(type(text))
84
+ # # Convert text to JSON forma
85
+
86
+ # formatted_result = re.sub("json", "", text)
87
+ # formatted_result = re.sub("`", "", formatted_result)
88
+
89
+ # res = json.loads(formatted_result)
90
+
91
+ # return res
92
+
93
+
94
+ @app.route("/item", methods=["POST"])
95
+ def generate():
96
+ try:
97
+ data = request.get_json()
98
+ # print("Received data:", data)
99
+ res = requests.post("http://localhost:5000/generate", json=data)
100
+ print("Response from model:", res.text)
101
+ return jsonify({"message": res.text})
102
+ except Exception as e:
103
+ print("Error occurred:", e)
104
+ return jsonify({"error": str(e)})
105
+
106
+
107
+ if __name__ == "__main__":
108
+ app.run(debug=True)