Update app.py
Browse files12343 unmodified lines8910111213141516114 unmodified lines13113213313413513622 unmodified lines159160161162163164from fastapi import FastAPI, Form, Requestfrom fastapi.responses import HTMLResponse, StreamingResponseimport os3 unmodified linesapp = FastAPI()# Load environment variablesMODEL = "gpt-4o-mini"API_URL = os.getenv("API_URL")DISABLED = os.getenv("DISABLED") == 'True'OPENAI_API_KEYS = os.getenv("OPENAI_API_KEYS", "").split(",")NUM_THREADS = int(os.getenv("NUM_THREADS", 1))114 unmodified linesif DISABLED:return StreamingResponse(iter(["Usage limit reached."]), media_type="text/plain")payload = {"model": MODEL,"messages": [{"role": "user", "content": input}],22 unmodified lineschunk_json = json.loads(chunk_data[6:])if "choices" in chunk_json and "delta" in chunk_json["choices"][0] and "content" in chunk_json["choices"][0]["delta"]:yield chunk_json["choices"][0]["delta"]["content"]except Exception as e:yield f"Error: {str(e)}"1233 unmodified lines789101112131415114 unmodified lines13013113213313413513613713813914014122 unmodified lines164165166167168169170171from fastapi import FastAPI, Form, Requestfrom fastapi.responses import HTMLResponse, StreamingResponseimport os3 unmodified linesapp = FastAPI()# Load environment variables with defaultsMODEL = "gpt-4o-mini"API_URL = os.getenv("API_URL", "https://api.openai.com/v1/chat/completions") # Default to OpenAI APIDISABLED = os.getenv("DISABLED") == 'True'OPENAI_API_KEYS = os.getenv("OPENAI_API_KEYS", "").split(",")NUM_THREADS = int(os.getenv("NUM_THREADS", 1))114 unmodified linesif DISABLED:return StreamingResponse(iter(["Usage limit reached."]), media_type="text/plain")if not API_URL:return StreamingResponse(iter(["Error: API_URL is not set in the environment."]), media_type="text/plain")if not OPENAI_API_KEYS or OPENAI_API_KEYS == [""]:return StreamingResponse(iter(["Error: No valid OPENAI_API_KEYS provided."]), media_type="text/plain")payload = {"model": MODEL,"messages": [{"role": "user", "content": input}],22 unmodified lineschunk_json = json.loads(chunk_data[6:])if "choices" in chunk_json and "delta" in chunk_json["choices"][0] and "content" in chunk_json["choices"][0]["delta"]:yield chunk_json["choices"][0]["delta"]["content"]except requests.exceptions.MissingSchema:yield "Error: Invalid API_URL. Please provide a valid URL (e.g., https://api.openai.com/v1/chat/completions)."except Exception as e:yield f"Error: {str(e)}"
app.py+10-3