clarity-ai 6.5.2 → 6.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Dockerfile +9 -0
- package/package.json +1 -1
- package/requirements.txt +3 -8
- package/space_app.py +22 -79
- package/src/providers/index.js +2 -2
package/Dockerfile
ADDED
package/package.json
CHANGED
package/requirements.txt
CHANGED
package/space_app.py
CHANGED
|
@@ -1,83 +1,26 @@
|
|
|
1
|
-
import
|
|
2
|
-
from
|
|
3
|
-
|
|
4
|
-
from fastapi.responses import StreamingResponse, JSONResponse
|
|
5
|
-
from contextlib import asynccontextmanager
|
|
1
|
+
import gradio as gr
|
|
2
|
+
from huggingface_hub import InferenceClient
|
|
3
|
+
import os
|
|
6
4
|
|
|
7
5
|
HF_TOKEN = os.environ.get('HF_TOKEN', '')
|
|
8
6
|
MODEL_NAME = os.environ.get('HF_MODEL', 'Clarity-flash-weights')
|
|
9
|
-
BASE_MODEL = os.environ.get('BASE_MODEL_ID', '')
|
|
10
|
-
DS = f'Universal-618/{MODEL_NAME}'
|
|
11
7
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
model = PeftModel.from_pretrained(base, path, token=HF_TOKEN)
|
|
32
|
-
tokenizer = AutoTokenizer.from_pretrained(path, token=HF_TOKEN)
|
|
33
|
-
if tokenizer.pad_token is None:
|
|
34
|
-
tokenizer.pad_token = tokenizer.eos_token
|
|
35
|
-
print('[startup] ready', flush=True)
|
|
36
|
-
|
|
37
|
-
load_model()
|
|
38
|
-
|
|
39
|
-
app = FastAPI()
|
|
40
|
-
|
|
41
|
-
@app.post('/v1/chat/completions')
|
|
42
|
-
async def generate(request: Request):
|
|
43
|
-
body = await request.json()
|
|
44
|
-
prompt = tokenizer.apply_chat_template(body['messages'], tokenize=False, add_generation_prompt=True)
|
|
45
|
-
inputs = tokenizer(prompt, return_tensors='pt')
|
|
46
|
-
stream = body.get('stream', True)
|
|
47
|
-
max_tokens = body.get('max_tokens', 4096)
|
|
48
|
-
gen_kwargs = dict(input_ids=inputs.input_ids, attention_mask=inputs.attention_mask,
|
|
49
|
-
max_new_tokens=max_tokens, temperature=body.get('temperature', 0.7),
|
|
50
|
-
do_sample=True, pad_token_id=tokenizer.pad_token_id)
|
|
51
|
-
if stream:
|
|
52
|
-
from transformers import TextIteratorStreamer
|
|
53
|
-
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
|
54
|
-
t = Thread(target=model.generate, kwargs={**gen_kwargs, 'streamer': streamer})
|
|
55
|
-
t.start()
|
|
56
|
-
async def gen():
|
|
57
|
-
yield 'data: {"choices":[{"delta":{"role":"assistant"},"index":0}]}\n\n'
|
|
58
|
-
for text in streamer:
|
|
59
|
-
if text:
|
|
60
|
-
yield f'data: {json.dumps({"choices":[{"delta":{"content":text},"index":0}]})}\n\n'
|
|
61
|
-
yield 'data: [DONE]\n\n'
|
|
62
|
-
return StreamingResponse(gen(), media_type='text/event-stream', headers={
|
|
63
|
-
'Cache-Control': 'no-cache', 'Connection': 'keep-alive', 'X-Accel-Buffering': 'no'})
|
|
64
|
-
out = model.generate(**gen_kwargs)
|
|
65
|
-
text = tokenizer.decode(out[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
|
|
66
|
-
return {'choices': [{'message': {'role': 'assistant', 'content': text}, 'index': 0}]}
|
|
67
|
-
|
|
68
|
-
@app.get('/main-data/{path:path}')
|
|
69
|
-
async def main_data(path: str):
|
|
70
|
-
path = path or 'index.json'
|
|
71
|
-
fp = f'/data/{path}'
|
|
72
|
-
if not os.path.exists(fp):
|
|
73
|
-
return JSONResponse({'error': 'not found'}, status_code=404)
|
|
74
|
-
with open(fp) as f:
|
|
75
|
-
return JSONResponse(json.load(f))
|
|
76
|
-
|
|
77
|
-
@app.get('/')
|
|
78
|
-
async def root():
|
|
79
|
-
return {'status': 'ok', 'model': MODEL_NAME}
|
|
80
|
-
|
|
81
|
-
if __name__ == '__main__':
|
|
82
|
-
import uvicorn
|
|
83
|
-
uvicorn.run(app, host='0.0.0.0', port=7860)
|
|
8
|
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
9
|
+
client = InferenceClient(token=HF_TOKEN, model=f'Universal-618/{MODEL_NAME}')
|
|
10
|
+
messages = [{"role": "system", "content": system_message}] + history + [{"role": "user", "content": message}]
|
|
11
|
+
response = ''
|
|
12
|
+
for chunk in client.chat_completion(messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p):
|
|
13
|
+
if chunk.choices and chunk.choices[0].delta.content:
|
|
14
|
+
response += chunk.choices[0].delta.content
|
|
15
|
+
yield response
|
|
16
|
+
|
|
17
|
+
gr.ChatInterface(
|
|
18
|
+
respond,
|
|
19
|
+
additional_inputs=[
|
|
20
|
+
gr.Textbox(value="You are a helpful assistant.", label="System message"),
|
|
21
|
+
gr.Slider(minimum=1, maximum=8192, value=4096, step=1, label="Max new tokens"),
|
|
22
|
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
|
23
|
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
|
24
|
+
],
|
|
25
|
+
title=f'CLARITY {MODEL_NAME}',
|
|
26
|
+
)
|
package/src/providers/index.js
CHANGED
|
@@ -3,12 +3,12 @@ import { streamResponse } from './streaming.js';
|
|
|
3
3
|
|
|
4
4
|
const MODEL_ROUTES = {
|
|
5
5
|
'Universal-618/Clarity-flash-weights': [
|
|
6
|
-
'https://universal-618-clarity-2.hf.space/v1/chat/completions',
|
|
6
|
+
process.env.HUGGINGFACE_ENDPOINT || 'https://universal-618-clarity-2.hf.space/v1/chat/completions',
|
|
7
7
|
'https://universal-618-clarity-4.hf.space/v1/chat/completions',
|
|
8
8
|
'https://universal-618-clarity-5.hf.space/v1/chat/completions',
|
|
9
9
|
],
|
|
10
10
|
'Universal-618/Clarity-heavy-weights': [
|
|
11
|
-
'https://universal-618-clarity-3.hf.space/v1/chat/completions',
|
|
11
|
+
process.env.HUGGINGFACE_ENDPOINT || 'https://universal-618-clarity-3.hf.space/v1/chat/completions',
|
|
12
12
|
'https://universal-618-clarity-5.hf.space/v1/chat/completions',
|
|
13
13
|
'https://universal-618-clarity-6.hf.space/v1/chat/completions',
|
|
14
14
|
],
|