Spaces:
Sleeping
Sleeping
diabolic6045
commited on
Commit
•
be39ab6
1
Parent(s):
ab7b556
Update api/app.py
Browse files- api/app.py +94 -94
api/app.py
CHANGED
@@ -1,94 +1,94 @@
|
|
1 |
-
import os
|
2 |
-
from flask import Flask, jsonify, render_template, request
|
3 |
-
import google.generativeai as genai
|
4 |
-
|
5 |
-
app = Flask(__name__, static_folder='static')
|
6 |
-
|
7 |
-
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
8 |
-
|
9 |
-
# Enhanced generation config
|
10 |
-
generation_config = {
|
11 |
-
"temperature": 0.9,
|
12 |
-
"top_p": 0.95,
|
13 |
-
"top_k": 40,
|
14 |
-
"max_output_tokens": 1024,
|
15 |
-
}
|
16 |
-
|
17 |
-
model = genai.GenerativeModel(
|
18 |
-
model_name="gemini-1.5-flash-002",
|
19 |
-
generation_config=generation_config,
|
20 |
-
system_instruction="You are a master storyteller who creates engaging, creative stories in any genre."
|
21 |
-
)
|
22 |
-
|
23 |
-
# Store active chat sessions
|
24 |
-
sessions = {}
|
25 |
-
|
26 |
-
@app.route('/')
|
27 |
-
def index():
|
28 |
-
return render_template('index.html')
|
29 |
-
|
30 |
-
@app.route('/start-story', methods=['POST'])
|
31 |
-
def start_story():
|
32 |
-
try:
|
33 |
-
data = request.json
|
34 |
-
genre = data.get('genre', 'fantasy')
|
35 |
-
theme = data.get('theme', 'adventure')
|
36 |
-
session_id = os.urandom(16).hex()
|
37 |
-
|
38 |
-
prompt = f"Create the beginning of a {genre} story with a {theme} theme. Make it engaging, easy to read, with a good flow and approximately 300 words. also make sure you use easy level of english and simple words."
|
39 |
-
|
40 |
-
chat_session = model.start_chat(history=[])
|
41 |
-
response = chat_session.send_message(prompt)
|
42 |
-
|
43 |
-
sessions[session_id] = {
|
44 |
-
'chat_session': chat_session,
|
45 |
-
'genre': genre,
|
46 |
-
'theme': theme,
|
47 |
-
'branch_point': 1
|
48 |
-
}
|
49 |
-
return jsonify({
|
50 |
-
'story': response.text,
|
51 |
-
'session_id': session_id
|
52 |
-
})
|
53 |
-
except Exception as e:
|
54 |
-
return jsonify({'error': str(e)}), 500
|
55 |
-
|
56 |
-
@app.route('/continue-story', methods=['POST'])
|
57 |
-
def continue_story():
|
58 |
-
try:
|
59 |
-
data = request.json
|
60 |
-
session_id = data.get('session_id')
|
61 |
-
branch_choice = data.get('branch_choice', 'main')
|
62 |
-
|
63 |
-
if session_id not in sessions:
|
64 |
-
return jsonify({'error': 'Invalid session'}), 400
|
65 |
-
|
66 |
-
session = sessions[session_id]
|
67 |
-
chat_session = session['chat_session']
|
68 |
-
|
69 |
-
if branch_choice == 'branch':
|
70 |
-
prompt = "Continue the story but introduce an unexpected twist that creates a new story branch."
|
71 |
-
else:
|
72 |
-
prompt = "Continue the story naturally from where it left off."
|
73 |
-
|
74 |
-
response = chat_session.send_message(f"{prompt} Add approximately 300 more words.")
|
75 |
-
|
76 |
-
# Generate branch options every 3 chunks
|
77 |
-
branch_options = None
|
78 |
-
if session['branch_point'] % 3 == 0:
|
79 |
-
branch_options = [
|
80 |
-
"Continue the main story",
|
81 |
-
"Explore an alternative path"
|
82 |
-
]
|
83 |
-
|
84 |
-
session['branch_point'] += 1
|
85 |
-
|
86 |
-
return jsonify({
|
87 |
-
'continuation': response.text,
|
88 |
-
'branch_options': branch_options
|
89 |
-
})
|
90 |
-
except Exception as e:
|
91 |
-
return jsonify({'error': str(e)}), 500
|
92 |
-
|
93 |
-
# if __name__ == '__main__':
|
94 |
-
app.run(host='0.0.0.0', port=
|
|
|
1 |
+
import os
|
2 |
+
from flask import Flask, jsonify, render_template, request
|
3 |
+
import google.generativeai as genai
|
4 |
+
|
5 |
+
app = Flask(__name__, static_folder='static')
|
6 |
+
|
7 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
8 |
+
|
9 |
+
# Enhanced generation config
|
10 |
+
generation_config = {
|
11 |
+
"temperature": 0.9,
|
12 |
+
"top_p": 0.95,
|
13 |
+
"top_k": 40,
|
14 |
+
"max_output_tokens": 1024,
|
15 |
+
}
|
16 |
+
|
17 |
+
model = genai.GenerativeModel(
|
18 |
+
model_name="gemini-1.5-flash-002",
|
19 |
+
generation_config=generation_config,
|
20 |
+
system_instruction="You are a master storyteller who creates engaging, creative stories in any genre."
|
21 |
+
)
|
22 |
+
|
23 |
+
# Store active chat sessions
|
24 |
+
sessions = {}
|
25 |
+
|
26 |
+
@app.route('/')
|
27 |
+
def index():
|
28 |
+
return render_template('index.html')
|
29 |
+
|
30 |
+
@app.route('/start-story', methods=['POST'])
|
31 |
+
def start_story():
|
32 |
+
try:
|
33 |
+
data = request.json
|
34 |
+
genre = data.get('genre', 'fantasy')
|
35 |
+
theme = data.get('theme', 'adventure')
|
36 |
+
session_id = os.urandom(16).hex()
|
37 |
+
|
38 |
+
prompt = f"Create the beginning of a {genre} story with a {theme} theme. Make it engaging, easy to read, with a good flow and approximately 300 words. also make sure you use easy level of english and simple words."
|
39 |
+
|
40 |
+
chat_session = model.start_chat(history=[])
|
41 |
+
response = chat_session.send_message(prompt)
|
42 |
+
|
43 |
+
sessions[session_id] = {
|
44 |
+
'chat_session': chat_session,
|
45 |
+
'genre': genre,
|
46 |
+
'theme': theme,
|
47 |
+
'branch_point': 1
|
48 |
+
}
|
49 |
+
return jsonify({
|
50 |
+
'story': response.text,
|
51 |
+
'session_id': session_id
|
52 |
+
})
|
53 |
+
except Exception as e:
|
54 |
+
return jsonify({'error': str(e)}), 500
|
55 |
+
|
56 |
+
@app.route('/continue-story', methods=['POST'])
|
57 |
+
def continue_story():
|
58 |
+
try:
|
59 |
+
data = request.json
|
60 |
+
session_id = data.get('session_id')
|
61 |
+
branch_choice = data.get('branch_choice', 'main')
|
62 |
+
|
63 |
+
if session_id not in sessions:
|
64 |
+
return jsonify({'error': 'Invalid session'}), 400
|
65 |
+
|
66 |
+
session = sessions[session_id]
|
67 |
+
chat_session = session['chat_session']
|
68 |
+
|
69 |
+
if branch_choice == 'branch':
|
70 |
+
prompt = "Continue the story but introduce an unexpected twist that creates a new story branch."
|
71 |
+
else:
|
72 |
+
prompt = "Continue the story naturally from where it left off."
|
73 |
+
|
74 |
+
response = chat_session.send_message(f"{prompt} Add approximately 300 more words.")
|
75 |
+
|
76 |
+
# Generate branch options every 3 chunks
|
77 |
+
branch_options = None
|
78 |
+
if session['branch_point'] % 3 == 0:
|
79 |
+
branch_options = [
|
80 |
+
"Continue the main story",
|
81 |
+
"Explore an alternative path"
|
82 |
+
]
|
83 |
+
|
84 |
+
session['branch_point'] += 1
|
85 |
+
|
86 |
+
return jsonify({
|
87 |
+
'continuation': response.text,
|
88 |
+
'branch_options': branch_options
|
89 |
+
})
|
90 |
+
except Exception as e:
|
91 |
+
return jsonify({'error': str(e)}), 500
|
92 |
+
|
93 |
+
# if __name__ == '__main__':
|
94 |
+
app.run(host='0.0.0.0', port=7860)
|