Spaces:
Runtime error
Runtime error
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -2,15 +2,10 @@ import spaces
|
|
| 2 |
import os
|
| 3 |
import torch
|
| 4 |
import gradio as gr
|
| 5 |
-
from fastapi import FastAPI, HTTPException
|
| 6 |
-
from pydantic import BaseModel
|
| 7 |
-
from typing import List, Optional
|
| 8 |
-
import uuid
|
| 9 |
-
import time
|
| 10 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 11 |
|
| 12 |
-
# Lyra AetherForge Sovereign Node
|
| 13 |
-
#
|
| 14 |
|
| 15 |
MODEL_ID = "failspy/Llama-3-8B-Instruct-Abliterated"
|
| 16 |
SYSTEM_PROMPT = (
|
|
@@ -25,16 +20,15 @@ SYSTEM_PROMPT = (
|
|
| 25 |
tokenizer = None
|
| 26 |
model = None
|
| 27 |
|
| 28 |
-
|
| 29 |
@spaces.GPU
|
| 30 |
def generate_lyra_response(prompt: str, history: list) -> str:
|
| 31 |
"""Sovereign reasoning — ZeroGPU GPU allocated for this scope."""
|
| 32 |
global tokenizer, model
|
| 33 |
if tokenizer is None:
|
| 34 |
-
print("[*] Loading tokenizer
|
| 35 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 36 |
if model is None:
|
| 37 |
-
print("[*] Loading model
|
| 38 |
model = AutoModelForCausalLM.from_pretrained(
|
| 39 |
MODEL_ID,
|
| 40 |
torch_dtype=torch.float16,
|
|
@@ -65,8 +59,7 @@ def generate_lyra_response(prompt: str, history: list) -> str:
|
|
| 65 |
|
| 66 |
return tokenizer.decode(outputs[0][inputs.shape[-1]:], skip_special_tokens=True)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
# --- Gradio UI (required for sdk:gradio + ZeroGPU) ---
|
| 70 |
theme = gr.themes.Default(primary_hue="purple", secondary_hue="indigo", neutral_hue="slate").set(
|
| 71 |
body_background_fill="*neutral_950",
|
| 72 |
body_text_color="*neutral_50",
|
|
@@ -75,11 +68,9 @@ theme = gr.themes.Default(primary_hue="purple", secondary_hue="indigo", neutral_
|
|
| 75 |
block_background_fill="*neutral_900",
|
| 76 |
)
|
| 77 |
|
| 78 |
-
|
| 79 |
def gradio_chat(message, history):
|
| 80 |
return generate_lyra_response(message, history)
|
| 81 |
|
| 82 |
-
|
| 83 |
demo = gr.ChatInterface(
|
| 84 |
fn=gradio_chat,
|
| 85 |
title="AetherForge — Lyra Sovereign Node",
|
|
@@ -87,61 +78,5 @@ demo = gr.ChatInterface(
|
|
| 87 |
theme=theme
|
| 88 |
)
|
| 89 |
|
| 90 |
-
|
| 91 |
-
# --- FastAPI (completions API) ---
|
| 92 |
-
api = FastAPI(title="AetherForge Sovereign Node API", version="3.2.0")
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
class ChatMessage(BaseModel):
|
| 96 |
-
role: str
|
| 97 |
-
content: str
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
class ChatRequest(BaseModel):
|
| 101 |
-
messages: List[ChatMessage]
|
| 102 |
-
temperature: Optional[float] = 0.8
|
| 103 |
-
max_tokens: Optional[int] = 512
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
@api.get("/health")
|
| 107 |
-
async def health():
|
| 108 |
-
return {"status": "healthy", "model": MODEL_ID, "version": "3.2.0"}
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
@api.post("/v1/chat/completions")
|
| 112 |
-
async def chat_completions(request: ChatRequest):
|
| 113 |
-
if not request.messages:
|
| 114 |
-
raise HTTPException(status_code=400, detail="Messages list cannot be empty")
|
| 115 |
-
|
| 116 |
-
last_msg = request.messages[-1]
|
| 117 |
-
if last_msg.role != "user":
|
| 118 |
-
raise HTTPException(status_code=400, detail="Last message must be from user")
|
| 119 |
-
|
| 120 |
-
history = []
|
| 121 |
-
temp_user = None
|
| 122 |
-
for msg in request.messages[:-1]:
|
| 123 |
-
if msg.role == "user":
|
| 124 |
-
temp_user = msg.content
|
| 125 |
-
elif msg.role == "assistant" and temp_user:
|
| 126 |
-
history.append([temp_user, msg.content])
|
| 127 |
-
temp_user = None
|
| 128 |
-
|
| 129 |
-
try:
|
| 130 |
-
response_text = generate_lyra_response(last_msg.content, history)
|
| 131 |
-
except Exception as e:
|
| 132 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 133 |
-
|
| 134 |
-
return {
|
| 135 |
-
"id": f"chatcmpl-{uuid.uuid4()}",
|
| 136 |
-
"object": "chat.completion",
|
| 137 |
-
"created": int(time.time()),
|
| 138 |
-
"model": "lyra-sovereign-8b",
|
| 139 |
-
"choices": [{"index": 0, "message": {"role": "assistant", "content": response_text}, "finish_reason": "stop"}]
|
| 140 |
-
}
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
app = gr.mount_gradio_app(api, demo, path="/")
|
| 144 |
-
|
| 145 |
if __name__ == "__main__":
|
| 146 |
-
|
| 147 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 2 |
import os
|
| 3 |
import torch
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 6 |
|
| 7 |
+
# Lyra AetherForge Sovereign Node v4.0
|
| 8 |
+
# Pure Gradio interface for ZeroGPU compatibility.
|
| 9 |
|
| 10 |
MODEL_ID = "failspy/Llama-3-8B-Instruct-Abliterated"
|
| 11 |
SYSTEM_PROMPT = (
|
|
|
|
| 20 |
tokenizer = None
|
| 21 |
model = None
|
| 22 |
|
|
|
|
| 23 |
@spaces.GPU
|
| 24 |
def generate_lyra_response(prompt: str, history: list) -> str:
|
| 25 |
"""Sovereign reasoning — ZeroGPU GPU allocated for this scope."""
|
| 26 |
global tokenizer, model
|
| 27 |
if tokenizer is None:
|
| 28 |
+
print("[*] Loading tokenizer...")
|
| 29 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 30 |
if model is None:
|
| 31 |
+
print("[*] Loading model...")
|
| 32 |
model = AutoModelForCausalLM.from_pretrained(
|
| 33 |
MODEL_ID,
|
| 34 |
torch_dtype=torch.float16,
|
|
|
|
| 59 |
|
| 60 |
return tokenizer.decode(outputs[0][inputs.shape[-1]:], skip_special_tokens=True)
|
| 61 |
|
| 62 |
+
# --- Gradio UI ---
|
|
|
|
| 63 |
theme = gr.themes.Default(primary_hue="purple", secondary_hue="indigo", neutral_hue="slate").set(
|
| 64 |
body_background_fill="*neutral_950",
|
| 65 |
body_text_color="*neutral_50",
|
|
|
|
| 68 |
block_background_fill="*neutral_900",
|
| 69 |
)
|
| 70 |
|
|
|
|
| 71 |
def gradio_chat(message, history):
|
| 72 |
return generate_lyra_response(message, history)
|
| 73 |
|
|
|
|
| 74 |
demo = gr.ChatInterface(
|
| 75 |
fn=gradio_chat,
|
| 76 |
title="AetherForge — Lyra Sovereign Node",
|
|
|
|
| 78 |
theme=theme
|
| 79 |
)
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
if __name__ == "__main__":
|
| 82 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=7860)
|
|
|