Spaces:
Sleeping
Sleeping
Update server/app.py
Browse files- server/app.py +27 -80
server/app.py
CHANGED
|
@@ -1,96 +1,43 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
-
from
|
| 3 |
-
import
|
| 4 |
-
from openai import OpenAI
|
| 5 |
-
import threading
|
| 6 |
-
import time
|
| 7 |
-
import requests
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
-
env = EmailEnv("hard")
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
key = os.environ.get("API_KEY")
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
return None
|
| 20 |
-
|
| 21 |
-
try:
|
| 22 |
-
client = OpenAI(base_url=base, api_key=key)
|
| 23 |
-
response = client.chat.completions.create(
|
| 24 |
-
model="gpt-4o-mini",
|
| 25 |
-
messages=[{"role": "user", "content": "Reply OK"}]
|
| 26 |
-
)
|
| 27 |
-
print(f"[{label}] Call succeeded: {response.choices[0].message.content}")
|
| 28 |
-
return response
|
| 29 |
-
except Exception as e:
|
| 30 |
-
print(f"[{label}] Call failed: {e}")
|
| 31 |
-
return None
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
def startup_llm_trigger():
|
| 35 |
-
time.sleep(3)
|
| 36 |
-
print("ENV CHECK →", {
|
| 37 |
-
"API_BASE_URL": os.environ.get("API_BASE_URL", "NOT SET"),
|
| 38 |
-
"API_KEY": "SET" if os.environ.get("API_KEY") else "NOT SET"
|
| 39 |
-
})
|
| 40 |
-
make_llm_call("startup-trigger")
|
| 41 |
-
|
| 42 |
-
@app.on_event("startup")
|
| 43 |
-
def startup():
|
| 44 |
-
# Fire startup LLM call in background after server is ready
|
| 45 |
-
threading.Thread(target=startup_llm_trigger, daemon=True).start()
|
| 46 |
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
@app.post("/reset")
|
| 49 |
def reset():
|
| 50 |
-
state
|
|
|
|
|
|
|
| 51 |
return {"state": state}
|
| 52 |
|
| 53 |
-
|
| 54 |
@app.post("/step")
|
| 55 |
-
def step(action:
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
return {
|
| 59 |
"state": state,
|
| 60 |
"reward": reward,
|
| 61 |
-
"done": done
|
| 62 |
-
|
| 63 |
-
}
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
@app.get("/")
|
| 67 |
-
def root():
|
| 68 |
-
base = os.environ.get("API_BASE_URL")
|
| 69 |
-
key = os.environ.get("API_KEY") or os.environ.get("HF_TOKEN")
|
| 70 |
-
|
| 71 |
-
print(f"API_BASE_URL: '{base}'")
|
| 72 |
-
print(f"API_KEY present: {bool(key)}")
|
| 73 |
-
|
| 74 |
-
if not key:
|
| 75 |
-
print("Missing API_KEY")
|
| 76 |
-
return {"message": "running", "llm": "unavailable"}
|
| 77 |
-
|
| 78 |
-
try:
|
| 79 |
-
client = OpenAI(base_url=base, api_key=key) # base can be None, OpenAI handles it
|
| 80 |
-
response = client.chat.completions.create(
|
| 81 |
-
model=os.environ.get("MODEL_NAME", "gpt-4o-mini"),
|
| 82 |
-
messages=[{"role": "user", "content": "Reply OK"}]
|
| 83 |
-
)
|
| 84 |
-
print("LLM call succeeded:", response.choices[0].message.content)
|
| 85 |
-
return {"message": "running", "llm": response.choices[0].message.content}
|
| 86 |
-
|
| 87 |
-
except BaseException as e:
|
| 88 |
-
print("LLM call failed:", repr(e))
|
| 89 |
-
return {"message": "running", "llm": "unavailable"}
|
| 90 |
-
def main():
|
| 91 |
-
import uvicorn
|
| 92 |
-
uvicorn.run("server:app", host="0.0.0.0", port=7860)
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
if __name__ == "__main__":
|
| 96 |
-
main()
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
app = FastAPI()
|
|
|
|
| 6 |
|
| 7 |
+
emails = [
|
| 8 |
+
{"email": "Refund my order", "label": "support"},
|
| 9 |
+
{"email": "Interested in pricing", "label": "sales"},
|
| 10 |
+
{"email": "Bug in product", "label": "support"},
|
| 11 |
+
{"email": "Partnership request", "label": "business"}
|
| 12 |
+
]
|
| 13 |
|
| 14 |
+
state = {}
|
| 15 |
+
done = False
|
|
|
|
| 16 |
|
| 17 |
+
class Action(BaseModel):
|
| 18 |
+
action: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
@app.get("/")
|
| 21 |
+
def root():
|
| 22 |
+
return {"status": "ok"}
|
| 23 |
|
| 24 |
@app.post("/reset")
|
| 25 |
def reset():
|
| 26 |
+
global state, done
|
| 27 |
+
state = random.choice(emails)
|
| 28 |
+
done = False
|
| 29 |
return {"state": state}
|
| 30 |
|
|
|
|
| 31 |
@app.post("/step")
|
| 32 |
+
def step(action: Action):
|
| 33 |
+
global state, done
|
| 34 |
+
|
| 35 |
+
correct = action.action == state["label"]
|
| 36 |
+
reward = 1.0 if correct else 0.0
|
| 37 |
+
done = True
|
| 38 |
+
|
| 39 |
return {
|
| 40 |
"state": state,
|
| 41 |
"reward": reward,
|
| 42 |
+
"done": done
|
| 43 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|