Update main.py
Browse files
main.py
CHANGED
|
@@ -1,10 +1,9 @@
|
|
| 1 |
import os
|
| 2 |
from fastapi import FastAPI, HTTPException
|
| 3 |
from pydantic import BaseModel
|
| 4 |
-
from transformers import
|
| 5 |
from huggingface_hub import login
|
| 6 |
|
| 7 |
-
|
| 8 |
# Define the FastAPI app
|
| 9 |
app = FastAPI()
|
| 10 |
|
|
@@ -12,27 +11,18 @@ class InputText(BaseModel):
|
|
| 12 |
input_text: str
|
| 13 |
temperature: float = 1.0 # Default temperature
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
login(token=HF_TOKEN)
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
model = AutoModelForCausalLM.from_pretrained("S1mp1eXXX/Mia_astral-1B")
|
| 22 |
|
| 23 |
def generate_text(prompt, max_length=1400, temperature=1.0):
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
input_ids,
|
| 27 |
-
max_length=max_length,
|
| 28 |
-
repetition_penalty=1.2,
|
| 29 |
-
do_sample=True,
|
| 30 |
-
top_k=50,
|
| 31 |
-
top_p=0.95,
|
| 32 |
-
temperature=temperature, # Set the temperature parameter
|
| 33 |
-
num_return_sequences=1
|
| 34 |
-
)
|
| 35 |
-
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 36 |
|
| 37 |
@app.post("/generate-text/")
|
| 38 |
async def generate_text_post(data: InputText):
|
|
|
|
| 1 |
import os
|
| 2 |
from fastapi import FastAPI, HTTPException
|
| 3 |
from pydantic import BaseModel
|
| 4 |
+
from transformers import pipeline
|
| 5 |
from huggingface_hub import login
|
| 6 |
|
|
|
|
| 7 |
# Define the FastAPI app
|
| 8 |
app = FastAPI()
|
| 9 |
|
|
|
|
| 11 |
input_text: str
|
| 12 |
temperature: float = 1.0 # Default temperature
|
| 13 |
|
| 14 |
+
# Hugging Face authentication
|
| 15 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # Replace with your actual token or set it as an environment variable
|
| 16 |
+
|
| 17 |
+
# Login to Hugging Face
|
| 18 |
login(token=HF_TOKEN)
|
| 19 |
|
| 20 |
+
# Define the text generation pipeline
|
| 21 |
+
text_generation_pipeline = pipeline("text-generation", model="S1mp1eXXX/Mia_astral-1B", use_auth_token=HF_TOKEN)
|
|
|
|
| 22 |
|
| 23 |
def generate_text(prompt, max_length=1400, temperature=1.0):
|
| 24 |
+
outputs = text_generation_pipeline(prompt, max_length=max_length, temperature=temperature, num_return_sequences=1)
|
| 25 |
+
return outputs[0]['generated_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
@app.post("/generate-text/")
|
| 28 |
async def generate_text_post(data: InputText):
|