Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,22 +17,17 @@ from huggingface_hub import InferenceClient
|
|
| 17 |
from fastapi import FastAPI, Request
|
| 18 |
import uvicorn
|
| 19 |
|
| 20 |
-
# ---
|
| 21 |
-
HF_TOKEN = os.getenv("HF_TOKEN") #
|
| 22 |
MODEL_NAME = "openai/gpt-oss-20b"
|
| 23 |
-
|
| 24 |
-
# --- Hidden system prompt ---
|
| 25 |
SYSTEM_MESSAGE = "You are Persaillia AI. You love coding and talking about airplanes."
|
| 26 |
-
|
| 27 |
-
# --- Hidden default parameters ---
|
| 28 |
DEFAULT_MAX_TOKENS = 512
|
| 29 |
DEFAULT_TEMPERATURE = 0.7
|
| 30 |
DEFAULT_TOP_P = 0.95
|
| 31 |
|
| 32 |
-
# --- Hugging Face client ---
|
| 33 |
client = InferenceClient(token=HF_TOKEN, model=MODEL_NAME)
|
| 34 |
|
| 35 |
-
# --- FastAPI
|
| 36 |
api = FastAPI()
|
| 37 |
|
| 38 |
@api.post("/chat")
|
|
@@ -41,7 +36,6 @@ async def chat_endpoint(req: Request):
|
|
| 41 |
user_message = body.get("message", "")
|
| 42 |
messages = [{"role": "system", "content": SYSTEM_MESSAGE},
|
| 43 |
{"role": "user", "content": user_message}]
|
| 44 |
-
|
| 45 |
completion = client.chat_completion(
|
| 46 |
messages,
|
| 47 |
max_tokens=DEFAULT_MAX_TOKENS,
|
|
@@ -51,14 +45,12 @@ async def chat_endpoint(req: Request):
|
|
| 51 |
reply = completion.choices[0].message["content"]
|
| 52 |
return {"reply": reply}
|
| 53 |
|
| 54 |
-
|
| 55 |
-
# --- Gradio Chat UI ---
|
| 56 |
def respond(message, history):
|
| 57 |
history = history or []
|
| 58 |
messages = [{"role": "system", "content": SYSTEM_MESSAGE}]
|
| 59 |
messages.extend(history)
|
| 60 |
messages.append({"role": "user", "content": message})
|
| 61 |
-
|
| 62 |
completion = client.chat_completion(
|
| 63 |
messages,
|
| 64 |
max_tokens=DEFAULT_MAX_TOKENS,
|
|
@@ -69,20 +61,15 @@ def respond(message, history):
|
|
| 69 |
history.append((message, reply))
|
| 70 |
return history, history
|
| 71 |
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
with gr.Row():
|
| 77 |
-
space_url = "https://huggingface.co/spaces/phaseDoutAI/persillia-AI"
|
| 78 |
-
gr.Markdown(f"π **Space URL:** [{space_url}]({space_url})")
|
| 79 |
-
chatbot = gr.ChatInterface(fn=respond, type="messages")
|
| 80 |
-
# Note: No .render() here to prevent duplicate flashing
|
| 81 |
-
|
| 82 |
|
| 83 |
-
# Mount Gradio
|
| 84 |
-
app = gr.mount_gradio_app(api,
|
| 85 |
|
| 86 |
if __name__ == "__main__":
|
| 87 |
-
print(f"β
Space URL: https://huggingface.co/spaces/phaseDoutAI/persillia-AI")
|
| 88 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 17 |
from fastapi import FastAPI, Request
|
| 18 |
import uvicorn
|
| 19 |
|
| 20 |
+
# --- Settings ---
|
| 21 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # Set this in Space secrets
|
| 22 |
MODEL_NAME = "openai/gpt-oss-20b"
|
|
|
|
|
|
|
| 23 |
SYSTEM_MESSAGE = "You are Persaillia AI. You love coding and talking about airplanes."
|
|
|
|
|
|
|
| 24 |
DEFAULT_MAX_TOKENS = 512
|
| 25 |
DEFAULT_TEMPERATURE = 0.7
|
| 26 |
DEFAULT_TOP_P = 0.95
|
| 27 |
|
|
|
|
| 28 |
client = InferenceClient(token=HF_TOKEN, model=MODEL_NAME)
|
| 29 |
|
| 30 |
+
# --- FastAPI for external API ---
|
| 31 |
api = FastAPI()
|
| 32 |
|
| 33 |
@api.post("/chat")
|
|
|
|
| 36 |
user_message = body.get("message", "")
|
| 37 |
messages = [{"role": "system", "content": SYSTEM_MESSAGE},
|
| 38 |
{"role": "user", "content": user_message}]
|
|
|
|
| 39 |
completion = client.chat_completion(
|
| 40 |
messages,
|
| 41 |
max_tokens=DEFAULT_MAX_TOKENS,
|
|
|
|
| 45 |
reply = completion.choices[0].message["content"]
|
| 46 |
return {"reply": reply}
|
| 47 |
|
| 48 |
+
# --- Gradio chat function ---
|
|
|
|
| 49 |
def respond(message, history):
|
| 50 |
history = history or []
|
| 51 |
messages = [{"role": "system", "content": SYSTEM_MESSAGE}]
|
| 52 |
messages.extend(history)
|
| 53 |
messages.append({"role": "user", "content": message})
|
|
|
|
| 54 |
completion = client.chat_completion(
|
| 55 |
messages,
|
| 56 |
max_tokens=DEFAULT_MAX_TOKENS,
|
|
|
|
| 61 |
history.append((message, reply))
|
| 62 |
return history, history
|
| 63 |
|
| 64 |
+
# --- Gradio ChatInterface (standalone, no extra Blocks) ---
|
| 65 |
+
chatbot = gr.ChatInterface(fn=respond, type="messages")
|
| 66 |
|
| 67 |
+
# Add Markdown above the chat (Space URL and title)
|
| 68 |
+
chatbot.blocks.insert(0, gr.Markdown("### π Persaillia AI β running on Hugging Face Spaces"))
|
| 69 |
+
chatbot.blocks.insert(1, gr.Markdown("π **Space URL:** [https://huggingface.co/spaces/phaseDoutAI/persillia-AI](https://huggingface.co/spaces/phaseDoutAI/persillia-AI)"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
# --- Mount Gradio to FastAPI ---
|
| 72 |
+
app = gr.mount_gradio_app(api, chatbot, path="/")
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
|
|
|
| 75 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|