This repository was archived by the owner on May 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
50 lines (37 loc) · 1.53 KB
/
Copy pathmain.py
File metadata and controls
50 lines (37 loc) · 1.53 KB
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
import json
import os
from langchain_core.messages import HumanMessage
from langchain_google_genai import ChatGoogleGenerativeAI
from flask import Flask, jsonify, request, send_file, send_from_directory
os.environ["GOOGLE_API_KEY"] = "API_Key";
app = Flask(__name__)
@app.route("/")
def index():
return send_file('web/index.html')
@app.route("/api/generate", methods=["POST"])
def generate_api():
if request.method == "POST":
if os.environ["GOOGLE_API_KEY"] == 'API_Key':
return jsonify({ "error": '''
Enter Your API Key in
main.py
'''.replace('\n', '') })
try:
req_body = request.get_json()
content = req_body.get("contents")+["if given question is not math question then simply say This is not math question.Please enter a math question. not more then it else solve and explain."]
model = ChatGoogleGenerativeAI(model=req_body.get("model"))
message = HumanMessage(
content=content
)
response = model.stream([message])
def stream():
for chunk in response:
yield 'data: %s\n\n' % json.dumps({ "text": chunk.content })
return stream(), {'Content-Type': 'text/event-stream'}
except Exception as e:
return jsonify({ "error": str(e) })
@app.route('/<path:path>')
def serve_static(path):
return send_from_directory('web', path)
if __name__ == "__main__":
app.run(port=int(os.environ.get('PORT', 80)))