Spaces:
Sleeping
Sleeping
implement tokenizer
Browse files- main.py +19 -7
- requirements.txt +2 -1
main.py
CHANGED
|
@@ -5,9 +5,11 @@ from fastapi import FastAPI
|
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
from typing import List
|
|
|
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
client = InferenceClient("openai-community/gpt2-medium")
|
|
|
|
| 11 |
|
| 12 |
SYSTEM_PROMPT = (
|
| 13 |
"You are a very powerful AI to generate interesting stories for short-form content "
|
|
@@ -15,6 +17,8 @@ SYSTEM_PROMPT = (
|
|
| 15 |
"Make sure to be engaging and creative in your responses."
|
| 16 |
)
|
| 17 |
|
|
|
|
|
|
|
| 18 |
class Item(BaseModel):
|
| 19 |
prompt: str
|
| 20 |
history: List[str] = []
|
|
@@ -23,7 +27,6 @@ class Item(BaseModel):
|
|
| 23 |
top_p: float = 0.15
|
| 24 |
repetition_penalty: float = 1.0
|
| 25 |
|
| 26 |
-
|
| 27 |
def format_prompt(message, history):
|
| 28 |
prompt = "<s>"
|
| 29 |
for user_prompt, bot_response in history:
|
|
@@ -31,16 +34,21 @@ def format_prompt(message, history):
|
|
| 31 |
prompt += f"[INST] {message} [/INST]"
|
| 32 |
return prompt
|
| 33 |
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def generate(item: Item):
|
| 36 |
temperature = max(float(item.temperature), 1e-2)
|
| 37 |
|
| 38 |
formatted_prompt = format_prompt(f"{SYSTEM_PROMPT}, {item.prompt}", item.history)
|
| 39 |
|
| 40 |
-
input_token_length =
|
| 41 |
-
max_allowed_tokens =
|
| 42 |
max_new_tokens = min(item.max_new_tokens, max_allowed_tokens)
|
| 43 |
|
|
|
|
|
|
|
|
|
|
| 44 |
stream = client.text_generation(
|
| 45 |
formatted_prompt,
|
| 46 |
temperature=temperature,
|
|
@@ -60,13 +68,12 @@ def generate(item: Item):
|
|
| 60 |
|
| 61 |
return output
|
| 62 |
|
| 63 |
-
|
| 64 |
@app.get("/generate/")
|
| 65 |
async def generate_text(
|
| 66 |
prompt: str,
|
| 67 |
history: List[str] = [],
|
| 68 |
temperature: float = 0.0,
|
| 69 |
-
max_new_tokens: int =
|
| 70 |
top_p: float = 0.15,
|
| 71 |
repetition_penalty: float = 1.0,
|
| 72 |
):
|
|
@@ -79,6 +86,11 @@ async def generate_text(
|
|
| 79 |
repetition_penalty=repetition_penalty,
|
| 80 |
)
|
| 81 |
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
|
|
|
|
|
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
from typing import List
|
| 8 |
+
import tiktoken
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
client = InferenceClient("openai-community/gpt2-medium")
|
| 12 |
+
tokenizer = tiktoken.get_encoding("gpt2")
|
| 13 |
|
| 14 |
SYSTEM_PROMPT = (
|
| 15 |
"You are a very powerful AI to generate interesting stories for short-form content "
|
|
|
|
| 17 |
"Make sure to be engaging and creative in your responses."
|
| 18 |
)
|
| 19 |
|
| 20 |
+
MAX_CONTEXT_LENGTH = 1024
|
| 21 |
+
|
| 22 |
class Item(BaseModel):
|
| 23 |
prompt: str
|
| 24 |
history: List[str] = []
|
|
|
|
| 27 |
top_p: float = 0.15
|
| 28 |
repetition_penalty: float = 1.0
|
| 29 |
|
|
|
|
| 30 |
def format_prompt(message, history):
|
| 31 |
prompt = "<s>"
|
| 32 |
for user_prompt, bot_response in history:
|
|
|
|
| 34 |
prompt += f"[INST] {message} [/INST]"
|
| 35 |
return prompt
|
| 36 |
|
| 37 |
+
def count_tokens(text):
|
| 38 |
+
return len(tokenizer.encode(text))
|
| 39 |
|
| 40 |
def generate(item: Item):
|
| 41 |
temperature = max(float(item.temperature), 1e-2)
|
| 42 |
|
| 43 |
formatted_prompt = format_prompt(f"{SYSTEM_PROMPT}, {item.prompt}", item.history)
|
| 44 |
|
| 45 |
+
input_token_length = count_tokens(formatted_prompt)
|
| 46 |
+
max_allowed_tokens = MAX_CONTEXT_LENGTH - input_token_length
|
| 47 |
max_new_tokens = min(item.max_new_tokens, max_allowed_tokens)
|
| 48 |
|
| 49 |
+
if max_new_tokens <= 0:
|
| 50 |
+
raise ValueError("The input is too long. Please reduce the prompt or history length.")
|
| 51 |
+
|
| 52 |
stream = client.text_generation(
|
| 53 |
formatted_prompt,
|
| 54 |
temperature=temperature,
|
|
|
|
| 68 |
|
| 69 |
return output
|
| 70 |
|
|
|
|
| 71 |
@app.get("/generate/")
|
| 72 |
async def generate_text(
|
| 73 |
prompt: str,
|
| 74 |
history: List[str] = [],
|
| 75 |
temperature: float = 0.0,
|
| 76 |
+
max_new_tokens: int = 1024,
|
| 77 |
top_p: float = 0.15,
|
| 78 |
repetition_penalty: float = 1.0,
|
| 79 |
):
|
|
|
|
| 86 |
repetition_penalty=repetition_penalty,
|
| 87 |
)
|
| 88 |
|
| 89 |
+
try:
|
| 90 |
+
response = await asyncio.to_thread(generate, item)
|
| 91 |
+
return {"response": response}
|
| 92 |
+
except ValueError as e:
|
| 93 |
+
return {"error": str(e)}
|
| 94 |
|
| 95 |
+
if __name__ == "__main__":
|
| 96 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
requirements.txt
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
fastapi
|
| 2 |
uvicorn
|
| 3 |
huggingface_hub
|
| 4 |
-
pydantic
|
|
|
|
|
|
| 1 |
fastapi
|
| 2 |
uvicorn
|
| 3 |
huggingface_hub
|
| 4 |
+
pydantic
|
| 5 |
+
tiktoken
|