Spaces:
Sleeping
Sleeping
import asyncio | |
import json | |
from flask_cors import CORS | |
from flask import Flask, request, jsonify | |
from waitress import serve | |
import concurrent.futures | |
import requests | |
import os | |
import google.generativeai as genai | |
app = Flask(__name__) | |
app.config['JSON_AS_ASCII'] = False | |
CORS(app, resources={r"/*": {"origins": "*"}}) | |
API_KEY = os.environ['API_KEY2'] | |
genai.configure(api_key=API_KEY) | |
safety_settings = [ | |
{ | |
"category": "HARM_CATEGORY_HARASSMENT", | |
"threshold": "BLOCK_NONE" | |
}, | |
{ | |
"category": "HARM_CATEGORY_HATE_SPEECH", | |
"threshold": "BLOCK_NONE" | |
}, | |
{ | |
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", | |
"threshold": "BLOCK_NONE" | |
}, | |
{ | |
"category": "HARM_CATEGORY_DANGEROUS_CONTENT", | |
"threshold": "BLOCK_NONE" | |
}, | |
] | |
model = genai.GenerativeModel( | |
model_name = "gemini-pro", | |
safety_settings = safety_settings, | |
generation_config = genai.GenerationConfig( | |
candidate_count = 1, | |
max_output_tokens = 4096, | |
temperature = 0.95, | |
top_p = 0.9, | |
top_k = 40 | |
) | |
) | |
def get_response(prompt:str): | |
try: | |
response = model.generate_content(prompt) | |
return response.text | |
except Exception as e: | |
return str(e) + '\nПочему:' + str(response.prompt_feedback) | |
def create_handler(name: str): | |
def handler(): | |
try: | |
if name == 'write': | |
prompt = request.json['prompt'] | |
try: | |
response = get_response(prompt) | |
return response | |
except Exception as e: | |
return str(e) | |
except Exception as error: | |
return jsonify({"error": str(error)}), 500 | |
handler.__name__ = name | |
return handler | |
app.route('/write', methods=['POST'])(create_handler('write')) | |