-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
66 lines (56 loc) · 1.77 KB
/
app.py
File metadata and controls
66 lines (56 loc) · 1.77 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import html
from flask import Flask, jsonify, make_response, render_template_string
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False
app.config['JSONIFY_MIMETYPE'] = 'application/json;charset=utf-8'
llm = OpenAI(temperature=0.7)
prompt = PromptTemplate(
input_variables=["product"],
template="{product}を作る会社の社名として、何かいいものはないですか?日本語の社名でお願いします。",
)
chain = LLMChain(llm=llm, prompt=prompt)
# HTMLテンプレートを文字列として定義
html_template = """
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI Response Test</title>
</head>
<body>
<h1>API Test Page</h1>
<div id="response"></div>
<script>
fetch('/api/hello')
.then(response => response.json())
.then(data => {
document.getElementById('response').textContent = data.message;
})
.catch(error => console.error('Error:', error));
</script>
</body>
</html>
"""
@app.route('/')
def index():
return render_template_string(html_template)
@app.route('/api/hello')
def hello():
prediction = chain.run("カラフルな靴下")
decoded_data = html.unescape(prediction.strip())
response = {
"message": decoded_data
}
resp = make_response(jsonify(response))
print(resp)
resp.headers['Content-Type'] = 'application/json; charset=utf-8'
return resp
def lambda_handler(event, context):
with app.app_context():
return hello().get_data(as_text=True)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=9000)