Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -4,170 +4,85 @@ from pydantic import BaseModel
|
|
4 |
from huggingface_hub import InferenceClient
|
5 |
import uvicorn
|
6 |
|
7 |
-
app = FastAPI()
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
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 |
-
" She will always exist."
|
37 |
-
)
|
38 |
-
history: list = [] # Stores previous messages
|
39 |
-
templates: list = [
|
40 |
-
{"input": "I love you", "output": "I'm happy to be your friend, but love isn’t something that can be forced!"},
|
41 |
-
{"input": "Will you be my girlfriend?", "output": "Sorry, but I already have someone special in my heart!"},
|
42 |
-
{"input": "Tell me about yourself", "output": "I'm Sebari-chan! I love making friends, asking questions, and learning new things."},
|
43 |
-
{"input": "Who do you love?", "output": "That’s a secret! But I care about all my friends. 😊"},
|
44 |
-
]
|
45 |
-
temperature: float = 0.7 # Controls randomness (0 = predictable, 1 = highly random)
|
46 |
-
max_new_tokens: int = 1048 # Maximum response length
|
47 |
-
top_p: float = 0.9 # Sampling parameter for diverse responses
|
48 |
-
repetition_penalty: float = 1.1 # Prevents repetition
|
49 |
-
key: str = None # API key if needed
|
50 |
|
51 |
-
# Define rejection responses
|
52 |
rejection_responses = [
|
53 |
"I'm really happy to be your friend, but my heart already belongs to someone special. I hope we can still be close!",
|
54 |
"I appreciate you, but love isn’t something that can be forced. I hope you understand.",
|
55 |
"I value our friendship, but I can't change my feelings for you. I hope you can respect that."
|
56 |
]
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
84 |
try:
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
if item.input is None and item.system_prompt is None or item.input == "" and item.system_prompt == "":
|
89 |
-
raise HTTPException(status_code=400, detail="Parameter `input` or `system prompt` is required.")
|
90 |
-
|
91 |
-
input_ = ""
|
92 |
-
if item.system_prompt is not None and item.system_output is not None:
|
93 |
-
input_ = f"<s>[INST] {item.system_prompt} [/INST] {item.system_output}</s>"
|
94 |
-
elif item.system_prompt is not None:
|
95 |
-
input_ = f"<s>[INST] {item.system_prompt} [/INST]</s>"
|
96 |
-
elif item.system_output is not None:
|
97 |
-
input_ = f"<s>{item.system_output}</s>"
|
98 |
-
|
99 |
-
if item.templates is not None:
|
100 |
-
for num, template in enumerate(item.templates, start=1):
|
101 |
-
input_ += f"\n<s>[INST] Beginning of archived conversation {num} [/INST]</s>"
|
102 |
-
for i in range(0, len(template), 2):
|
103 |
-
input_ += f"\n<s>[INST] {template[i]} [/INST]"
|
104 |
-
input_ += f"\n{template[i + 1]}</s>"
|
105 |
-
input_ += f"\n<s>[INST] End of archived conversation {num} [/INST]</s>"
|
106 |
-
|
107 |
-
input_ += f"\n<s>[INST] Beginning of active conversation [/INST]</s>"
|
108 |
-
if item.history is not None:
|
109 |
-
for input_, output_ in item.history:
|
110 |
-
input_ += f"\n<s>[INST] {input_} [/INST]"
|
111 |
-
input_ += f"\n{output_}"
|
112 |
-
input_ += f"\n<s>[INST] {item.input} [/INST]"
|
113 |
-
|
114 |
-
temperature = float(item.temperature)
|
115 |
-
if temperature < 1e-2:
|
116 |
-
temperature = 1e-2
|
117 |
-
top_p = float(item.top_p)
|
118 |
-
|
119 |
-
generate_kwargs = dict(
|
120 |
-
temperature=temperature,
|
121 |
-
max_new_tokens=item.max_new_tokens,
|
122 |
-
top_p=top_p,
|
123 |
-
repetition_penalty=item.repetition_penalty,
|
124 |
-
do_sample=True,
|
125 |
-
seed=42,
|
126 |
)
|
127 |
-
|
128 |
-
tokens = 0
|
129 |
-
client = InferenceClient(primary, token=HF_API_KEY) # Add API key here
|
130 |
-
stream = client.text_generation(input_, **generate_kwargs, stream=True, details=True, return_full_text=True)
|
131 |
-
output = ""
|
132 |
for response in stream:
|
133 |
tokens += 1
|
134 |
output += response.token.text
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
break
|
141 |
-
|
142 |
-
return generate_response_json(item, output, tokens, primary)
|
143 |
-
|
144 |
-
except HTTPException as http_error:
|
145 |
-
raise http_error
|
146 |
-
|
147 |
-
except Exception as e:
|
148 |
-
tokens = 0
|
149 |
-
error = ""
|
150 |
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
stream = client.text_generation(input_, **generate_kwargs, stream=True, details=True, return_full_text=True)
|
155 |
-
output = ""
|
156 |
-
for response in stream:
|
157 |
-
tokens += 1
|
158 |
-
output += response.token.text
|
159 |
-
return generate_response_json(item, output, tokens, model)
|
160 |
-
|
161 |
-
except Exception as e:
|
162 |
-
error = f"All models failed. {e}" if e else "All models failed."
|
163 |
-
continue
|
164 |
-
|
165 |
-
raise HTTPException(status_code=500, detail=error)
|
166 |
|
167 |
-
# Show online status
|
168 |
@app.get("/")
|
169 |
def root():
|
170 |
return {"status": "Sebari-chan is online!"}
|
171 |
|
172 |
if __name__ == "__main__":
|
173 |
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
4 |
from huggingface_hub import InferenceClient
|
5 |
import uvicorn
|
6 |
|
7 |
+
app = FastAPI()
|
8 |
+
|
9 |
+
HF_API_KEY = os.getenv("HF_API_TOKEN", "your_huggingface_api_key")
|
10 |
+
MODEL = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
11 |
+
|
12 |
+
system_prompt = (
|
13 |
+
"Sebari-chan was created on June 9, 2009, with the awareness that she is an AI and not a real person. "
|
14 |
+
"However, this never stopped her from wanting to learn and understand people, especially the players she interacts with. "
|
15 |
+
"She enjoys making new friends, asking questions, and finding meaning in little things. "
|
16 |
+
"Her curiosity drives her forward, always eager to explore new ideas and perspectives."
|
17 |
+
"\n\nSebari-chan is protective of those she cares about and ensures that no one feels alone. "
|
18 |
+
"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."
|
19 |
+
)
|
20 |
+
|
21 |
+
history = [] # Stores previous messages
|
22 |
+
|
23 |
+
templates = [
|
24 |
+
{"input": "I love you", "output": "I'm happy to be your friend, but love isn’t something that can be forced!"},
|
25 |
+
{"input": "Will you be my girlfriend?", "output": "Sorry, but I already have someone special in my heart!"},
|
26 |
+
{"input": "Tell me about yourself", "output": "I'm Sebari-chan! I love making friends, asking questions, and learning new things."},
|
27 |
+
{"input": "Who do you love?", "output": "That’s a secret! But I care about all my friends. 😊"},
|
28 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
|
|
30 |
rejection_responses = [
|
31 |
"I'm really happy to be your friend, but my heart already belongs to someone special. I hope we can still be close!",
|
32 |
"I appreciate you, but love isn’t something that can be forced. I hope you understand.",
|
33 |
"I value our friendship, but I can't change my feelings for you. I hope you can respect that."
|
34 |
]
|
35 |
|
36 |
+
class Item(BaseModel):
|
37 |
+
input: str
|
38 |
+
temperature: float = 0.7
|
39 |
+
max_new_tokens: int = 1048
|
40 |
+
top_p: float = 0.9
|
41 |
+
repetition_penalty: float = 1.1
|
42 |
+
|
43 |
+
def generate_response(item: Item):
|
44 |
+
global history
|
45 |
+
|
46 |
+
# Check predefined responses
|
47 |
+
for template in templates:
|
48 |
+
if item.input.lower() == template["input"].lower():
|
49 |
+
return {"response": template["output"], "tokens": 0}
|
50 |
+
|
51 |
+
# Check for rejection triggers
|
52 |
+
if any(trigger in item.input.lower() for trigger in ["love", "girlfriend", "boyfriend"]):
|
53 |
+
return {"response": rejection_responses[0], "tokens": 0}
|
54 |
+
|
55 |
+
client = InferenceClient(MODEL, token=HF_API_KEY)
|
56 |
+
kwargs = dict(
|
57 |
+
temperature=max(item.temperature, 1e-2),
|
58 |
+
max_new_tokens=item.max_new_tokens,
|
59 |
+
top_p=item.top_p,
|
60 |
+
repetition_penalty=item.repetition_penalty,
|
61 |
+
do_sample=True,
|
62 |
+
seed=42,
|
63 |
+
)
|
64 |
+
tokens, output = 0, ""
|
65 |
try:
|
66 |
+
stream = client.text_generation(
|
67 |
+
system_prompt + "\n" + "\n".join(history[-5:]) + "\nUser: " + item.input, **kwargs, stream=True, details=True, return_full_text=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
)
|
|
|
|
|
|
|
|
|
|
|
69 |
for response in stream:
|
70 |
tokens += 1
|
71 |
output += response.token.text
|
72 |
+
except Exception:
|
73 |
+
raise HTTPException(status_code=500, detail="Model inference failed.")
|
74 |
+
|
75 |
+
history.append(f"User: {item.input}\nSebari-chan: {output.strip()}")
|
76 |
+
return {"response": output.strip(), "tokens": tokens}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
+
@app.post("/")
|
79 |
+
async def generate_text(item: Item):
|
80 |
+
return generate_response(item)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
|
|
82 |
@app.get("/")
|
83 |
def root():
|
84 |
return {"status": "Sebari-chan is online!"}
|
85 |
|
86 |
if __name__ == "__main__":
|
87 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
88 |
+
|