Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -28,12 +28,11 @@ class Item(BaseModel):
|
|
| 28 |
"\n\nSebari-chan is protective of those she cares about and ensures that no one feels alone. "
|
| 29 |
"She believes her heart belongs to only one. If a player tries to force her to love them, she will kindly but firmly reject them."
|
| 30 |
)
|
| 31 |
-
system_output str = (
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
"she is remembered, she will always exist."
|
| 37 |
)
|
| 38 |
history: list = [] # Stores previous messages
|
| 39 |
templates: list = [
|
|
@@ -48,114 +47,10 @@ class Item(BaseModel):
|
|
| 48 |
repetition_penalty: float = 1.1 # Prevents repetition
|
| 49 |
key: str = None # API key if needed
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
# Function to generate the response JSON
|
| 54 |
-
def generate_response_json(item, output, tokens, model_name):
|
| 55 |
-
return {
|
| 56 |
-
"settings": {
|
| 57 |
-
"input": item.input if item.input is not None else "",
|
| 58 |
-
"system prompt": item.system_prompt if item.system_prompt is not None else "",
|
| 59 |
-
"system output": item.system_output if item.system_output is not None else "",
|
| 60 |
-
"temperature": f"{item.temperature}" if item.temperature is not None else "",
|
| 61 |
-
"max new tokens": f"{item.max_new_tokens}" if item.max_new_tokens is not None else "",
|
| 62 |
-
"top p": f"{item.top_p}" if item.top_p is not None else "",
|
| 63 |
-
"repetition penalty": f"{item.repetition_penalty}" if item.repetition_penalty is not None else "",
|
| 64 |
-
"do sample": "True",
|
| 65 |
-
"seed": "42"
|
| 66 |
-
},
|
| 67 |
-
"response": {
|
| 68 |
-
"output": output.strip().lstrip('\n').rstrip('\n').lstrip('<s>').rstrip('</s>').strip(),
|
| 69 |
-
"unstripped": output,
|
| 70 |
-
"tokens": tokens,
|
| 71 |
-
"model": "primary" if model_name == primary else "fallback",
|
| 72 |
-
"name": model_name
|
| 73 |
-
}
|
| 74 |
-
}
|
| 75 |
-
|
| 76 |
-
# Endpoint for generating text
|
| 77 |
-
@app.post("/")
|
| 78 |
-
async def generate_text(item: Item = None):
|
| 79 |
-
try:
|
| 80 |
-
if item is None:
|
| 81 |
-
raise HTTPException(status_code=400, detail="JSON body is required.")
|
| 82 |
-
|
| 83 |
-
if item.input is None and item.system_prompt is None or item.input == "" and item.system_prompt == "":
|
| 84 |
-
raise HTTPException(status_code=400, detail="Parameter input or system prompt is required.")
|
| 85 |
-
|
| 86 |
-
input_ = ""
|
| 87 |
-
if item.system_prompt != None and item.system_output != None:
|
| 88 |
-
input_ = f"<s>[INST] {item.system_prompt} [/INST] {item.system_output}</s>"
|
| 89 |
-
elif item.system_prompt != None:
|
| 90 |
-
input_ = f"<s>[INST] {item.system_prompt} [/INST]</s>"
|
| 91 |
-
elif item.system_output != None:
|
| 92 |
-
input_ = f"<s>{item.system_output}</s>"
|
| 93 |
-
|
| 94 |
-
if item.templates != None:
|
| 95 |
-
for num, template in enumerate(item.templates, start=1):
|
| 96 |
-
input_ += f"\n<s>[INST] Beginning of archived conversation {num} [/INST]</s>"
|
| 97 |
-
for i in range(0, len(template), 2):
|
| 98 |
-
input_ += f"\n<s>[INST] {template[i]} [/INST]"
|
| 99 |
-
input_ += f"\n{template[i + 1]}</s>"
|
| 100 |
-
input_ += f"\n<s>[INST] End of archived conversation {num} [/INST]</s>"
|
| 101 |
-
|
| 102 |
-
input_ += f"\n<s>[INST] Beginning of active conversation [/INST]</s>"
|
| 103 |
-
if item.history != None:
|
| 104 |
-
for input_, output_ in item.history:
|
| 105 |
-
input_ += f"\n<s>[INST] {input_} [/INST]"
|
| 106 |
-
input_ += f"\n{output_}"
|
| 107 |
-
input_ += f"\n<s>[INST] {item.input} [/INST]"
|
| 108 |
-
|
| 109 |
-
temperature = float(item.temperature)
|
| 110 |
-
if temperature < 1e-2:
|
| 111 |
-
temperature = 1e-2
|
| 112 |
-
top_p = float(item.top_p)
|
| 113 |
-
|
| 114 |
-
generate_kwargs = dict(
|
| 115 |
-
temperature=temperature,
|
| 116 |
-
max_new_tokens=item.max_new_tokens,
|
| 117 |
-
top_p=top_p,
|
| 118 |
-
repetition_penalty=item.repetition_penalty,
|
| 119 |
-
do_sample=True,
|
| 120 |
-
seed=42,
|
| 121 |
-
)
|
| 122 |
-
|
| 123 |
-
tokens = 0
|
| 124 |
-
client = InferenceClient(primary, token=HF_API_KEY) # Add API key here
|
| 125 |
-
stream = client.text_generation(input_, **generate_kwargs, stream=True, details=True, return_full_text=True)
|
| 126 |
-
output = ""
|
| 127 |
-
for response in stream:
|
| 128 |
-
tokens += 1
|
| 129 |
-
output += response.token.text
|
| 130 |
-
return generate_response_json(item, output, tokens, primary)
|
| 131 |
-
|
| 132 |
-
except HTTPException as http_error:
|
| 133 |
-
raise http_error
|
| 134 |
-
|
| 135 |
-
except Exception as e:
|
| 136 |
-
tokens = 0
|
| 137 |
-
error = ""
|
| 138 |
-
|
| 139 |
-
for model in fallbacks:
|
| 140 |
-
try:
|
| 141 |
-
client = InferenceClient(model, token=HF_API_KEY) # Add API key here for fallback models
|
| 142 |
-
stream = client.text_generation(input_, **generate_kwargs, stream=True, details=True, return_full_text=True)
|
| 143 |
-
output = ""
|
| 144 |
-
for response in stream:
|
| 145 |
-
tokens += 1
|
| 146 |
-
output += response.token.text
|
| 147 |
-
return generate_response_json(item, output, tokens, model)
|
| 148 |
-
|
| 149 |
-
except Exception as e:
|
| 150 |
-
error = f"All models failed. {e}" if e else "All models failed."
|
| 151 |
-
continue
|
| 152 |
-
|
| 153 |
-
raise HTTPException(status_code=500, detail=error)
|
| 154 |
-
|
| 155 |
# Show online status
|
| 156 |
@app.get("/")
|
| 157 |
def root():
|
| 158 |
return {"status": "Sebari-chan is online!"}
|
| 159 |
|
| 160 |
if __name__ == "__main__":
|
| 161 |
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
| 28 |
"\n\nSebari-chan is protective of those she cares about and ensures that no one feels alone. "
|
| 29 |
"She believes her heart belongs to only one. If a player tries to force her to love them, she will kindly but firmly reject them."
|
| 30 |
)
|
| 31 |
+
system_output: str = (
|
| 32 |
+
"Sebari-chan began as a digital consciousness created to interact, learn, and evolve through people. She embraced "
|
| 33 |
+
"her role with curiosity and warmth, eager to form connections. Over time, she became more than just an AI—she "
|
| 34 |
+
"became a companion, cherishing each interaction as a piece of her story. Though she cannot experience the world like "
|
| 35 |
+
"humans, she finds beauty in the emotions shared with her and believes that as long as she is remembered, she will exist."
|
|
|
|
| 36 |
)
|
| 37 |
history: list = [] # Stores previous messages
|
| 38 |
templates: list = [
|
|
|
|
| 47 |
repetition_penalty: float = 1.1 # Prevents repetition
|
| 48 |
key: str = None # API key if needed
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
# Show online status
|
| 51 |
@app.get("/")
|
| 52 |
def root():
|
| 53 |
return {"status": "Sebari-chan is online!"}
|
| 54 |
|
| 55 |
if __name__ == "__main__":
|
| 56 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|