import os import shutil import uuid import json from typing import Dict from fastapi import FastAPI, HTTPException, Request from fastapi.responses import JSONResponse import requests import uvicorn import gradio app = FastAPI() @app.get('/generate-id') async def generate_id(): try: id = str(uuid.uuid4()) chat_folder_path = os.path.join(os.path.dirname(__file__), 'histories', id) os.makedirs(chat_folder_path, exist_ok=True) return JSONResponse(content={"id": id}, status_code=200) except Exception as e: return JSONResponse(content={"status": False, "error": "Internal server error"}, status_code=500) # Define the endpoint for /chat @app.post("/chat") async def chat(data: Dict): try: chatid = data.get('chatid') role = data.get('role') message = data.get('message') if not all([chatid, role, message]): raise HTTPException(status_code=400, detail='Missing required parameters "role" or "message"') chat_folder_path = os.path.join(os.path.dirname(__file__), 'histories', chatid) user_role_folder = role[:50] user_folder_path = os.path.join(chat_folder_path, 'users', user_role_folder) messages_file_path = os.path.join(user_folder_path, 'messages.json') if not os.path.exists(chat_folder_path): raise HTTPException(status_code=404, detail='Chat ID not found. Generate a new Chat.') if not os.path.exists(user_folder_path): os.makedirs(user_folder_path, exist_ok=True) messages = [] if os.path.exists(messages_file_path): with open(messages_file_path, 'r') as file: messages = json.load(file) system_role_exists = any(msg['role'] == 'system' for msg in messages) if not system_role_exists: messages.append({"role": "system", "content": role}) messages.append({"role": "user", "content": message}) response = requests.post('https://kastg-hf-llm-api.hf.space/api/v1/chat/completions', json={ "model": "nous-mixtral-8x7b", "messages": messages, "temperature": 1.5, "top_p": 0.1, "max_tokens": -1, "use_cache": False, "stream": False }) response_data = response.json() assistant_response = response_data['choices'][0]['message']['content'] messages.append({"role": "assistant", "content": assistant_response}) with open(messages_file_path, 'w') as file: json.dump(messages, file) return {"success": True, "response": assistant_response} except Exception as e: raise HTTPException(status_code=400, detail=str(e)) @app.delete('/reset/{chatid}') async def reset(chatid: str): try: if not chatid: raise HTTPException(status_code=400, detail='Missing chatid parameter') chat_folder_path = os.path.join(os.path.dirname(__file__), 'histories', chatid) if not os.path.exists(chat_folder_path): raise HTTPException(status_code=404, detail='Chat ID not found') for item in os.listdir(chat_folder_path): item_path = os.path.join(chat_folder_path, item) if os.path.isdir(item_path): shutil.rmtree(item_path) else: os.remove(item_path) return {"success": True, "message": "Contents deleted successfully"} except Exception as e: raise HTTPException(status_code=500, detail='Internal server error') # Define the endpoint for /delete @app.delete('/delete') async def delete(chatid: str): try: if not chatid: raise HTTPException(status_code=400, detail='Missing chatid parameter') chat_folder_path = os.path.join(os.path.dirname(__file__), 'histories', chatid) if not os.path.exists(chat_folder_path): raise HTTPException(status_code=404, detail='Chat ID not found') shutil.rmtree(chat_folder_path) return JSONResponse(content={"success": True, "message": "Chat deleted successfully"}, status_code=200) except Exception as e: return JSONResponse(content={"error": "Internal server error"}, status_code=500) if __name__ == '__main__': uvicorn.run(app, host="0.0.0.0", port=)