Spaces:
Runtime error
Runtime error
dorogan commited on
Commit ·
1e2a35a
1
Parent(s): de83ed6
Update: basic methods and endpoints were added
Browse filesFiles changed (4)
- DockerfileAdded+11
- app.pyAdded+27
- model.pyAdded+24
- requirements.txtAdded+8
1234567891011FROM python:3.10-buster
WORKDIR /app/commandr-api-local
COPY . .
RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt
RUN python3 app.py123456789101112131415161718192021222324252627import uvicornfrom fastapi import FastAPIfrom pydantic import BaseModelfrom model import get_answer_from_llm
class Prompt(BaseModel): prompt: str = ''
app = FastAPI( title='CommandRLLMAPI')
@app.post("/completion/")def get_answer(question: Prompt.prompt): answer = get_answer_from_llm(question) return answer
if __name__ == '__main__': uvicorn.run( app, host='0.0.0.0', port=8081 )123456789101112131415161718192021222324from transformers import AutoTokenizer, AutoModelForCausalLMimport torch
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model_id = "CohereForAI/c4ai-command-r-v01-4bit"tokenizer = AutoTokenizer.from_pretrained(model_id)model = AutoModelForCausalLM.from_pretrained(model_id).to(device)## <BOS_TOKEN><|START_OF_TURN_TOKEN|><|USER_TOKEN|>Hello, how are you?<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>
async def get_answer_from_llm(question: str = None): # Format message with the command-r chat template messages = [{"role": "user", "content": f"{question}"}] input_ids = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt") gen_tokens = model.generate( input_ids, max_new_tokens=100, do_sample=True, temperature=0.3, )
gen_text = await tokenizer.decode(gen_tokens[0]) return gen_text12345678torchtransformers>=4.39.1bitsandbytesacceleratetokenizerspydanticfastapiuvicornDockerfileAdded+11
app.pyAdded+27
model.pyAdded+24
requirements.txtAdded+8