Spaces:
Building
Building
Update app.py
Browse files
app.py
CHANGED
@@ -1,162 +1,27 @@
|
|
1 |
-
from fastapi import FastAPI
|
2 |
-
|
3 |
-
import
|
4 |
-
|
5 |
-
from
|
6 |
-
from
|
7 |
-
from
|
8 |
-
from
|
9 |
-
from
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
app =
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
@app.get("/")
|
31 |
-
def health_check():
|
32 |
-
return {"status": "ok"}
|
33 |
-
|
34 |
-
|
35 |
-
@app.post("/start_chat")
|
36 |
-
def start_chat(request: Request):
|
37 |
-
project_name = request.query_params.get("project_name")
|
38 |
-
if not project_name:
|
39 |
-
return {"error": "Missing project_name parameter."}
|
40 |
-
|
41 |
-
session = session_store.create_session(project_name)
|
42 |
-
return {"session_id": session.session_id}
|
43 |
-
|
44 |
-
|
45 |
-
@app.post("/chat")
|
46 |
-
async def chat(request: Request, x_session_id: str = Header(None)):
|
47 |
-
if not x_session_id:
|
48 |
-
return {"error": "Missing X-Session-ID header."}
|
49 |
-
|
50 |
-
session = session_store.get_session(x_session_id)
|
51 |
-
if not session:
|
52 |
-
return {"error": "Invalid or expired session."}
|
53 |
-
|
54 |
-
try:
|
55 |
-
body = await request.json()
|
56 |
-
user_input = body.get("user_input", "").strip()
|
57 |
-
if not user_input:
|
58 |
-
return {"error": "Empty user input."}
|
59 |
-
|
60 |
-
session.chat_history.append({"role": "user", "content": user_input})
|
61 |
-
project_name = session.project_name
|
62 |
-
|
63 |
-
if session.state == "intent_detection":
|
64 |
-
prompt = prompt_engine.build_intent_prompt(project_name)
|
65 |
-
llm_response = llm_connector.call_spark(project_name, prompt, session.chat_history)
|
66 |
-
if llm_response is None:
|
67 |
-
return {"error": "Failed to get intent detection result."}
|
68 |
-
|
69 |
-
intent = llm_response.get("intent")
|
70 |
-
params = llm_response.get("params", {})
|
71 |
-
missing = llm_response.get("missing", [])
|
72 |
-
|
73 |
-
session.last_intent = intent
|
74 |
-
session.variables.update(params)
|
75 |
-
session.awaiting_parameters = missing
|
76 |
-
|
77 |
-
if missing:
|
78 |
-
session.state = "parameter_extraction"
|
79 |
-
return {"response": f"Please provide: {', '.join(missing)}"}
|
80 |
-
|
81 |
-
session.state = "validation"
|
82 |
-
|
83 |
-
if session.state == "parameter_extraction":
|
84 |
-
prompt = prompt_engine.build_parameter_prompt(project_name, session.last_intent, session.awaiting_parameters)
|
85 |
-
llm_response = llm_connector.call_spark(project_name, prompt, session.chat_history)
|
86 |
-
if llm_response is None:
|
87 |
-
return {"error": "Failed to extract parameters."}
|
88 |
-
|
89 |
-
params = llm_response.get("params", {})
|
90 |
-
missing = llm_response.get("missing", [])
|
91 |
-
|
92 |
-
session.variables.update(params)
|
93 |
-
session.awaiting_parameters = missing
|
94 |
-
|
95 |
-
if missing:
|
96 |
-
return {"response": f"Please provide: {', '.join(missing)}"}
|
97 |
-
|
98 |
-
session.state = "validation"
|
99 |
-
|
100 |
-
if session.state == "validation":
|
101 |
-
intent_def = next((i for i in service_config.get_project_intents(project_name) if i["name"] == session.last_intent), None)
|
102 |
-
if not intent_def:
|
103 |
-
return {"error": f"Intent definition not found: {session.last_intent}"}
|
104 |
-
|
105 |
-
is_valid, errors = validation_engine.validate_parameters(intent_def, session.variables)
|
106 |
-
if not is_valid:
|
107 |
-
return {"response": " ".join(errors)}
|
108 |
-
|
109 |
-
session.state = "api_call"
|
110 |
-
|
111 |
-
if session.state == "api_call":
|
112 |
-
intent_def = next((i for i in service_config.get_project_intents(project_name) if i["name"] == session.last_intent), None)
|
113 |
-
api_response = api_connector.call_api(intent_def, session)
|
114 |
-
if "fallback" in api_response:
|
115 |
-
return {"response": api_response["fallback"]}
|
116 |
-
|
117 |
-
session.state = "humanization"
|
118 |
-
session.variables["api_result"] = api_response
|
119 |
-
|
120 |
-
if session.state == "humanization":
|
121 |
-
prompt = prompt_engine.build_humanization_prompt(project_name, session.last_intent)
|
122 |
-
chat_history = [{"role": "system", "content": str(session.variables["api_result"])}]
|
123 |
-
humanized_response = llm_connector.call_spark(project_name, prompt, chat_history)
|
124 |
-
if humanized_response is None:
|
125 |
-
return {"error": "Failed to humanize response."}
|
126 |
-
|
127 |
-
session.chat_history.append({"role": "assistant", "content": humanized_response.get("answer")})
|
128 |
-
session.state = "intent_detection" # reset state
|
129 |
-
session.last_intent = None
|
130 |
-
session.variables = {}
|
131 |
-
session.awaiting_parameters = []
|
132 |
-
|
133 |
-
return {"response": humanized_response.get("answer")}
|
134 |
-
|
135 |
-
except Exception as e:
|
136 |
-
log(f"❌ Error in chat: {e}")
|
137 |
-
traceback.print_exc()
|
138 |
-
return JSONResponse(content={"error": str(e)}, status_code=500)
|
139 |
-
|
140 |
-
|
141 |
-
@app.post("/reload_config")
|
142 |
-
def reload_config():
|
143 |
-
def background_reload():
|
144 |
-
try:
|
145 |
-
service_config.load()
|
146 |
-
log("✅ Service config reloaded successfully.")
|
147 |
-
except Exception as e:
|
148 |
-
log(f"❌ Error reloading config: {e}")
|
149 |
-
|
150 |
-
threading.Thread(target=background_reload, daemon=True).start()
|
151 |
-
return {"status": "accepted", "message": "Config reload started in background."}
|
152 |
-
|
153 |
-
|
154 |
-
@app.post("/run_tests")
|
155 |
-
def run_tests():
|
156 |
-
log("🚦 /run_tests endpoint called. (Test runner needs to be implemented.)")
|
157 |
-
return {"status": "not_implemented", "message": "Test runner is not yet implemented."}
|
158 |
-
|
159 |
-
|
160 |
-
if __name__ == "__main__":
|
161 |
-
log("🌐 Starting Flare Intent Service...")
|
162 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
import uvicorn
|
3 |
+
from log import log
|
4 |
+
|
5 |
+
from auth_controller import router as auth_router
|
6 |
+
from config_controller import router as config_router
|
7 |
+
from project_controller import router as project_router
|
8 |
+
from spark_controller import router as spark_router
|
9 |
+
from test_controller import router as test_router
|
10 |
+
|
11 |
+
# Sağlık kontrolü
|
12 |
+
app = FastAPI()
|
13 |
+
|
14 |
+
@app.get("/")
|
15 |
+
def health_check():
|
16 |
+
return {"status": "ok"}
|
17 |
+
|
18 |
+
# Router eklemeleri
|
19 |
+
app.include_router(auth_router, prefix="/auth")
|
20 |
+
app.include_router(config_router, prefix="/config")
|
21 |
+
app.include_router(project_router, prefix="/project")
|
22 |
+
app.include_router(spark_router, prefix="/spark")
|
23 |
+
app.include_router(test_router, prefix="/test")
|
24 |
+
|
25 |
+
if __name__ == "__main__":
|
26 |
+
log("🌐 Starting Flare UI Backend...")
|
27 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|