|
| 1 | +import torch |
| 2 | +from transformers import AutoTokenizer, AutoModel, AutoModelForCausalLM, pipeline |
| 3 | +import argparse |
| 4 | +from fastapi import FastAPI, Request |
| 5 | +import uvicorn, json, datetime |
| 6 | +import nest_asyncio |
| 7 | +from pyngrok import ngrok |
| 8 | + |
| 9 | +DEVICE = "cuda" |
| 10 | +DEVICE_ID = "0" |
| 11 | +CUDA_DEVICE = f"{DEVICE}:{DEVICE_ID}" if DEVICE_ID else DEVICE |
| 12 | + |
| 13 | + |
| 14 | +def torch_gc(): |
| 15 | + if torch.cuda.is_available(): |
| 16 | + with torch.cuda.device(CUDA_DEVICE): |
| 17 | + torch.cuda.empty_cache() |
| 18 | + torch.cuda.ipc_collect() |
| 19 | + |
| 20 | + |
| 21 | +app = FastAPI() |
| 22 | + |
| 23 | + |
| 24 | +@app.post("/") |
| 25 | +async def create_item(request: Request): |
| 26 | + global model, tokenizer, prompt_template |
| 27 | + json_post_raw = await request.json() |
| 28 | + json_post = json.dumps(json_post_raw) |
| 29 | + json_post_list = json.loads(json_post) |
| 30 | + question = json_post_list.get('prompt') |
| 31 | + prompt = prompt_template.format( |
| 32 | + user_question=question.replace("#","") |
| 33 | + ) |
| 34 | + sql_type = "自然语言转换成SQL查询" |
| 35 | + if sql_type in prompt: |
| 36 | + prompt += "```sql" |
| 37 | + else: |
| 38 | + prompt += ">>>" |
| 39 | + history = json_post_list.get('history') |
| 40 | + max_length = json_post_list.get('max_length') |
| 41 | + top_p = json_post_list.get('top_p') |
| 42 | + temperature = json_post_list.get('temperature') |
| 43 | + eos_token_id = tokenizer.convert_tokens_to_ids(["```"])[0] |
| 44 | + print("Loading a model and generating a SQL query for answering your question...") |
| 45 | + pipe = pipeline( |
| 46 | + "text-generation", |
| 47 | + model=model, |
| 48 | + tokenizer=tokenizer, |
| 49 | + max_new_tokens=300, |
| 50 | + do_sample=False, |
| 51 | + num_beams=5, # do beam search with 5 beams for high quality results |
| 52 | + ) |
| 53 | + print("==========input========") |
| 54 | + print(prompt) |
| 55 | + generated_query = ( |
| 56 | + pipe( |
| 57 | + prompt, |
| 58 | + num_return_sequences=1, |
| 59 | + eos_token_id=eos_token_id, |
| 60 | + pad_token_id=eos_token_id, |
| 61 | + )[0]["generated_text"] |
| 62 | + ) |
| 63 | + |
| 64 | + response = generated_query |
| 65 | + |
| 66 | + if sql_type in prompt: |
| 67 | + response = response.split("`sql")[-1].split("`")[0].split(";")[0].strip() + ";" |
| 68 | + |
| 69 | + else: |
| 70 | + response = response.split(">>>")[-1].split("`")[0].strip() |
| 71 | + |
| 72 | + print("========output========") |
| 73 | + print(response) |
| 74 | + torch_gc() |
| 75 | + return response |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == '__main__': |
| 79 | + prompt_template = "" |
| 80 | + with open("prompt.md", "r") as f: |
| 81 | + prompt_template = f.read() |
| 82 | + tokenizer = AutoTokenizer.from_pretrained("/mnt/workspace/sqlcoder-model/sqlcoder", trust_remote_code=True) |
| 83 | + model = AutoModelForCausalLM.from_pretrained("/mnt/workspace/sqlcoder-model/sqlcoder", |
| 84 | + trust_remote_code=True, |
| 85 | + # torch_dtype=torch.float16, |
| 86 | + load_in_8bit=True, |
| 87 | + device_map="auto", |
| 88 | + use_cache=True) |
| 89 | + ngrok_tunnel = ngrok.connect(8000) |
| 90 | + print('Public URL:', ngrok_tunnel.public_url) |
| 91 | + nest_asyncio.apply() |
| 92 | + uvicorn.run(app, host='0.0.0.0', port=8000, workers=1) |
| 93 | + |
| 94 | + |
| 95 | + |
0 commit comments