File size: 1,821 Bytes
1321ee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39b20ee
48a4271
1321ee7
48a4271
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3431f6b
48a4271
 
 
 
 
 
 
 
 
1321ee7
 
 
 
00a803a
1321ee7
143fe88
1321ee7
 
 
 
 
b5013ab
37b9e0d
 
 
 
8a7fc55
1321ee7
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
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'))