Spaces:
Paused
Paused
Download main.py from Dtel/mixtral_convox: direct link, hf CLI and curl.
- Browser
- Download file 1.31 kB
-
https://huggingface.co/spaces/Dtel/mixtral_convox/resolve/main/main.py
- Command line
-
hf download hf://spaces/Dtel/mixtral_convox/main.py
-
curl -L -o main.py https://huggingface.co/spaces/Dtel/mixtral_convox/resolve/main/main.py
1.31 kB
| from fastapi import FastAPI, HTTPException | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| import torch | |
| import os | |
| from pydantic import BaseModel | |
| app = FastAPI() | |
| # Get Hugging Face Token from Secrets | |
| hf_token = os.getenv("HUGGINGFACE_TOKEN") | |
| if not hf_token: | |
| raise ValueError("Hugging Face token is missing. Add it to Secrets.") | |
| # Load Model | |
| model_name = "mistralai/Mistral-7B-Instruct-v0.3" | |
| tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=hf_token) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| model_name, | |
| use_auth_token=hf_token, | |
| torch_dtype=torch.float16, | |
| device_map="auto" | |
| ) | |
| # Define request body schema | |
| class GenerateRequest(BaseModel): | |
| prompt: str | |
| max_length: int = 100 # Default value | |
| async def generate_text(request: GenerateRequest): | |
| try: | |
| inputs = tokenizer(request.prompt, return_tensors="pt").to("cuda") | |
| outputs = model.generate(inputs.input_ids, max_length=request.max_length, num_return_sequences=1) | |
| generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| return {"generated_text": generated_text} | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def health_check(): | |
| return {"status": "ok"} | |