-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
176 lines (158 loc) · 6.28 KB
/
app.py
File metadata and controls
176 lines (158 loc) · 6.28 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import os
import subprocess
import requests
import time
import json
from flask import Flask, request, jsonify, send_from_directory, render_template
MODEL_FALLBACK = 'mistral'
OLLAMA_HOST = 'http://localhost:11434'
app = Flask(__name__, static_folder='static', template_folder='templates')
# D&D 5e statblock JSON schema updated for better structure and UI alignment
STATBLOCK_SCHEMA = {
"type": "object",
"properties": {
"name": {"type": "string"},
"size_type": {"type": "string"},
"alignment": {"type": "string"},
"armor_class": {"type": "string"},
"hit_points": {"type": "string"},
"speed": {"type": "string"},
"strength": {"type": "integer"},
"dexterity": {"type": "integer"},
"constitution": {"type": "integer"},
"intelligence": {"type": "integer"},
"wisdom": {"type": "integer"},
"charisma": {"type": "integer"},
"skills": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {"type": "string"},
"desc": {"type": "string"}
},
"required": ["name", "desc"]
}
},
"saving_throws": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {"type": "string"},
"desc": {"type": "string"}
},
"required": ["name", "desc"]
}
},
"senses": {"type": "string"},
"languages": {"type": "string"},
"challenge": {"type": "string"},
"damage_vulnerabilities": {"type": "string"},
"damage_resistances": {"type": "string"},
"damage_immunities": {"type": "string"},
"condition_immunities": {"type": "string"},
"abilities": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {"type": "string"},
"desc": {"type": "string"}
},
"required": ["name", "desc"]
}
},
"actions": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {"type": "string"},
"desc": {"type": "string"}
},
"required": ["name", "desc"]
}
}
},
"required": [
"name", "size_type", "alignment", "armor_class", "hit_points", "speed",
"strength", "dexterity", "constitution", "intelligence", "wisdom", "charisma",
"skills", "saving_throws", "senses", "languages", "challenge",
"damage_vulnerabilities", "damage_resistances", "damage_immunities",
"condition_immunities", "abilities", "actions"
]
}
def ensure_ollama_server():
try:
requests.get(OLLAMA_HOST + "/api/tags", timeout=1)
print("Ollama backend already running.")
return None
except Exception:
print("Ollama not running. Starting ollama serve...")
proc = subprocess.Popen(["ollama", "serve"])
for _ in range(30):
try:
requests.get(OLLAMA_HOST + "/api/tags", timeout=1)
print("Ollama started successfully.")
return proc
except Exception:
time.sleep(1)
raise RuntimeError("Ollama did not start in time.")
ollama_proc = ensure_ollama_server()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/static/<path:filename>')
def staticfiles(filename):
return send_from_directory('static', filename)
@app.route('/generate', methods=['POST'])
def generate():
data = request.json
prompt = data.get('prompt', '').strip()
model = data.get('model', MODEL_FALLBACK)
if not prompt:
return jsonify({'error': 'No prompt provided'}), 400
# Ensure the prompt guides the AI towards the desired JSON structure
full_prompt = (
f"Generate a D&D 5e statblock for the following creature concept: '{prompt}'. "
"The output must be a single JSON object that strictly adheres to the provided schema. "
"For 'skills' and 'saving_throws', provide the proficiency bonus (e.g., '+5'). "
"For 'abilities' and 'actions', the 'name' is the title and the 'desc' is the full description. "
"For damage and condition immunities/resistances/vulnerabilities, provide a comma-separated string (e.g., 'fire, poison'). "
"If a field is not applicable, provide an empty string or empty list."
)
ollama_payload = {
"model": model,
"messages": [{"role": "user", "content": full_prompt}],
"stream": False,
"format": STATBLOCK_SCHEMA
}
try:
resp = requests.post(f"{OLLAMA_HOST}/api/chat", json=ollama_payload, timeout=120)
resp.raise_for_status()
result = resp.json()
content = result.get("message", {}).get("content", "")
if isinstance(content, str):
try:
# The content should already be a JSON string, so parse it
statblock = json.loads(content)
except json.JSONDecodeError as e:
# If parsing fails, it's a generation error
error_message = f"Ollama returned malformed JSON. Error: {e}. Raw content: {content}"
print(error_message)
return jsonify({'error': error_message}), 500
else:
# If it's already a dict, which is unexpected but possible
statblock = content
if not isinstance(statblock, dict) or 'name' not in statblock:
return jsonify({'error': 'AI failed to generate a valid statblock dictionary.'}), 500
except requests.exceptions.RequestException as e:
return jsonify({'error': str(e)}), 500
except Exception as e:
return jsonify({'error': f"An unexpected error occurred: {e}"}), 500
return jsonify({
"statblock": statblock
})
if __name__ == "__main__":
app.run(port=5000, host='0.0.0.0')