Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import json
|
| 3 |
+
from flask_cors import CORS
|
| 4 |
+
from flask import Flask, request, jsonify
|
| 5 |
+
from waitress import serve
|
| 6 |
+
import concurrent.futures
|
| 7 |
+
import requests
|
| 8 |
+
|
| 9 |
+
import os
|
| 10 |
+
import google.generativeai as genai
|
| 11 |
+
|
| 12 |
+
app = Flask(__name__)
|
| 13 |
+
app.config['JSON_AS_ASCII'] = False
|
| 14 |
+
CORS(app, resources={r"/*": {"origins": "*"}})
|
| 15 |
+
|
| 16 |
+
API_KEY = os.environ['API_KEY']
|
| 17 |
+
genai.configure(api_key=API_KEY)
|
| 18 |
+
model = genai.GenerativeModel('gemini-pro')
|
| 19 |
+
|
| 20 |
+
def get_response(prompt:str):
|
| 21 |
+
try:
|
| 22 |
+
response = model.generate_content(prompt)
|
| 23 |
+
return response
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return str(e)
|
| 26 |
+
|
| 27 |
+
def create_handler(name: str):
|
| 28 |
+
def handler():
|
| 29 |
+
try:
|
| 30 |
+
if name == 'write':
|
| 31 |
+
response = get_response(prompt)
|
| 32 |
+
return response
|
| 33 |
+
|
| 34 |
+
except Exception as error:
|
| 35 |
+
return jsonify({"error": str(error)}), 500
|
| 36 |
+
|
| 37 |
+
handler.__name__ = name
|
| 38 |
+
return handler
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
app.route('/write', methods=['POST'])(create_handler('write'))
|
| 42 |
+
|