ML-server / index.py
Thillai's picture
Create index.py
0273fe2 verified
import json
import google.generativeai as genai
import os
import re
from flask import Flask, jsonify, request
from flask_cors import CORS
import requests
app = Flask(__name__)
CORS(app)
# Set up the model
generation_config = {
"temperature": 0.9,
"top_p": 1,
"top_k": 1,
"max_output_tokens": 2048,
}
safety_settings = [
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
]
# model.generate_content("Elon Musk")
@app.route("/generate", methods=["POST"])
def generate_func():
api_key = "AIzaSyCV-2nkHNGMZ-IK84YYdltW2C9tkoUD9so"
genai.configure(api_key=api_key)
model = genai.GenerativeModel(model_name="gemini-1.0-pro",
generation_config=generation_config,
safety_settings=safety_settings)
numberOfQuestions = 5
selectedOption = "MCQ"
difficultyLevel = "medium"
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."
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)
response = model.generate_content(prompt)
formatted_result = re.sub("json", "", response.text)
res = re.sub("`", "", formatted_result)
try :
data = json.loads(res)
except Exception as e:
print(e)
return jsonify({"error": str(e)})
first_key = next(iter(data), None)
if first_key == "questions":
return jsonify(data[first_key])
else:
return jsonify(data)
@app.route("/home")
def read_root():
return { "hello": "world"}
# @app.route("/items",methods=["POST"])
# async def read_item():
# text = generate()
# print(type(text))
# # Convert text to JSON forma
# formatted_result = re.sub("json", "", text)
# formatted_result = re.sub("`", "", formatted_result)
# res = json.loads(formatted_result)
# return res
@app.route("/item", methods=["POST"])
def generate():
try:
data = request.get_json()
# print("Received data:", data)
res = requests.post("http://localhost:5000/generate", json=data)
print("Response from model:", res.text)
return jsonify({"message": res.text})
except Exception as e:
print("Error occurred:", e)
return jsonify({"error": str(e)})
if __name__ == "__main__":
app.run(debug=True)