Spaces:
Sleeping
Sleeping
Robin
commited on
Commit
·
6aab2ec
1
Parent(s):
6592996
static files order
Browse files
app.py
CHANGED
|
@@ -24,61 +24,60 @@ app.add_middleware(
|
|
| 24 |
allow_headers=["*"],
|
| 25 |
)
|
| 26 |
|
| 27 |
-
# Mount static files
|
| 28 |
-
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
| 29 |
-
|
| 30 |
class Message(BaseModel):
|
| 31 |
message: str
|
| 32 |
|
| 33 |
def generate_text(message: Message):
|
| 34 |
# Load existing chat history
|
| 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 |
@app.post("/api/generate-text")
|
| 77 |
async def inference(message: Message):
|
| 78 |
return generate_text(message=message)
|
| 79 |
|
| 80 |
-
|
| 81 |
@app.get("/chat-history", tags=["History"])
|
| 82 |
def get_chat_history(request: Request):
|
| 83 |
chat_history = load_chat_history()
|
| 84 |
return {"chat_history": chat_history}
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
allow_headers=["*"],
|
| 25 |
)
|
| 26 |
|
|
|
|
|
|
|
|
|
|
| 27 |
class Message(BaseModel):
|
| 28 |
message: str
|
| 29 |
|
| 30 |
def generate_text(message: Message):
|
| 31 |
# Load existing chat history
|
| 32 |
+
chat_history = load_chat_history()
|
| 33 |
|
| 34 |
+
# Add user message to history
|
| 35 |
+
chat_history = update_chat_history(chat_history, user_message=message.message)
|
| 36 |
|
| 37 |
+
# Format the prompt
|
| 38 |
+
formatted_prompt = instruction_prompt.format(
|
| 39 |
+
hints=hints,
|
| 40 |
+
chat_history=chat_history,
|
| 41 |
+
character=trump_character,
|
| 42 |
+
rules=game_rules,
|
| 43 |
+
triggers=triggers
|
| 44 |
+
)
|
| 45 |
|
| 46 |
+
# Get Character's response
|
| 47 |
+
chat_response = client.chat.complete(
|
| 48 |
+
model=model,
|
| 49 |
+
messages=[
|
| 50 |
+
{
|
| 51 |
+
"role": "system",
|
| 52 |
+
"content": formatted_prompt
|
| 53 |
+
},
|
| 54 |
+
{
|
| 55 |
+
"role": "user",
|
| 56 |
+
"content": message.message
|
| 57 |
+
}
|
| 58 |
+
]
|
| 59 |
+
)
|
| 60 |
+
clean_response = chat_response.choices[0].message.content
|
| 61 |
|
| 62 |
+
# Add character response to history
|
| 63 |
+
chat_history = update_chat_history(chat_history, character_response=clean_response)
|
| 64 |
|
| 65 |
+
# Save updated chat history
|
| 66 |
+
save_chat_history(chat_history)
|
| 67 |
|
| 68 |
+
return {
|
| 69 |
+
"character_response": clean_response,
|
| 70 |
+
"chat_history": chat_history
|
| 71 |
+
}
|
| 72 |
|
| 73 |
@app.post("/api/generate-text")
|
| 74 |
async def inference(message: Message):
|
| 75 |
return generate_text(message=message)
|
| 76 |
|
|
|
|
| 77 |
@app.get("/chat-history", tags=["History"])
|
| 78 |
def get_chat_history(request: Request):
|
| 79 |
chat_history = load_chat_history()
|
| 80 |
return {"chat_history": chat_history}
|
| 81 |
+
|
| 82 |
+
# Mount static files AFTER defining API routes
|
| 83 |
+
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|