Spaces:
Sleeping
Sleeping
no message
Browse files- main.py +19 -16
- requirements.txt +2 -1
main.py
CHANGED
@@ -4,18 +4,23 @@ from pydantic import BaseModel
|
|
4 |
from huggingface_hub import InferenceClient
|
5 |
import uvicorn
|
6 |
from typing import Generator
|
7 |
-
import json
|
8 |
import nltk
|
9 |
import os
|
|
|
10 |
|
11 |
-
|
12 |
nltk.data.path.append(os.getenv('NLTK_DATA'))
|
13 |
|
|
|
14 |
app = FastAPI()
|
15 |
|
16 |
# Initialize the InferenceClient with your model
|
17 |
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.2")
|
18 |
|
|
|
|
|
|
|
19 |
class Item(BaseModel):
|
20 |
prompt: str
|
21 |
history: list
|
@@ -25,23 +30,24 @@ class Item(BaseModel):
|
|
25 |
top_p: float = 0.15
|
26 |
repetition_penalty: float = 1.0
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
def format_prompt(current_prompt, history):
|
29 |
formatted_history = "<s>"
|
30 |
-
|
31 |
-
if entry["role"] == "user":
|
32 |
-
formatted_history += f"[USER] {entry['content']} [/USER]"
|
33 |
-
elif entry["role"] == "assistant":
|
34 |
-
formatted_history += f"[ASSISTANT] {entry['content']} [/ASSISTANT]"
|
35 |
formatted_history += f"[USER] {current_prompt} [/USER]</s>"
|
36 |
return formatted_history
|
37 |
|
38 |
-
|
39 |
def generate_stream(item: Item) -> Generator[bytes, None, None]:
|
40 |
-
|
41 |
-
|
42 |
-
input_token_count = len(nltk.word_tokenize(formatted_prompt))
|
43 |
|
44 |
-
# Ensure total token count doesn't exceed the maximum limit
|
45 |
max_tokens_allowed = 32768
|
46 |
max_new_tokens_adjusted = max(1, min(item.max_new_tokens, max_tokens_allowed - input_token_count))
|
47 |
|
@@ -54,18 +60,15 @@ def generate_stream(item: Item) -> Generator[bytes, None, None]:
|
|
54 |
"seed": 42,
|
55 |
}
|
56 |
|
57 |
-
# Stream the response from the InferenceClient
|
58 |
for response in client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True):
|
59 |
-
# This assumes 'details=True' gives you a structure where you can access the text like this
|
60 |
chunk = {
|
61 |
"text": response.token.text,
|
62 |
-
"complete": response.generated_text is not None
|
63 |
}
|
64 |
yield json.dumps(chunk).encode("utf-8") + b"\n"
|
65 |
|
66 |
@app.post("/generate/")
|
67 |
async def generate_text(item: Item):
|
68 |
-
# Stream response back to the client
|
69 |
return StreamingResponse(generate_stream(item), media_type="application/x-ndjson")
|
70 |
|
71 |
if __name__ == "__main__":
|
|
|
4 |
from huggingface_hub import InferenceClient
|
5 |
import uvicorn
|
6 |
from typing import Generator
|
7 |
+
import json
|
8 |
import nltk
|
9 |
import os
|
10 |
+
from transformers import pipeline
|
11 |
|
12 |
+
# Set up the environment for NLTK
|
13 |
nltk.data.path.append(os.getenv('NLTK_DATA'))
|
14 |
|
15 |
+
# Initialize the FastAPI app
|
16 |
app = FastAPI()
|
17 |
|
18 |
# Initialize the InferenceClient with your model
|
19 |
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.2")
|
20 |
|
21 |
+
# Initialize the summarization pipeline
|
22 |
+
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
23 |
+
|
24 |
class Item(BaseModel):
|
25 |
prompt: str
|
26 |
history: list
|
|
|
30 |
top_p: float = 0.15
|
31 |
repetition_penalty: float = 1.0
|
32 |
|
33 |
+
def summarize_history(history):
|
34 |
+
# Concatenate all history entries into a single string
|
35 |
+
full_history = " ".join(entry['content'] for entry in history if entry['role'] == 'user')
|
36 |
+
# Summarize the history
|
37 |
+
summarized_history = summarizer(full_history, max_length=1024, truncation=True)
|
38 |
+
return summarized_history[0]['summary_text']
|
39 |
+
|
40 |
def format_prompt(current_prompt, history):
|
41 |
formatted_history = "<s>"
|
42 |
+
formatted_history += f"[HISTORY] {history} [/HISTORY]"
|
|
|
|
|
|
|
|
|
43 |
formatted_history += f"[USER] {current_prompt} [/USER]</s>"
|
44 |
return formatted_history
|
45 |
|
|
|
46 |
def generate_stream(item: Item) -> Generator[bytes, None, None]:
|
47 |
+
summarized_history = summarize_history(item.history)
|
48 |
+
formatted_prompt = format_prompt(item.prompt, summarized_history)
|
49 |
+
input_token_count = len(nltk.word_tokenize(formatted_prompt))
|
50 |
|
|
|
51 |
max_tokens_allowed = 32768
|
52 |
max_new_tokens_adjusted = max(1, min(item.max_new_tokens, max_tokens_allowed - input_token_count))
|
53 |
|
|
|
60 |
"seed": 42,
|
61 |
}
|
62 |
|
|
|
63 |
for response in client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True):
|
|
|
64 |
chunk = {
|
65 |
"text": response.token.text,
|
66 |
+
"complete": response.generated_text is not None
|
67 |
}
|
68 |
yield json.dumps(chunk).encode("utf-8") + b"\n"
|
69 |
|
70 |
@app.post("/generate/")
|
71 |
async def generate_text(item: Item):
|
|
|
72 |
return StreamingResponse(generate_stream(item), media_type="application/x-ndjson")
|
73 |
|
74 |
if __name__ == "__main__":
|
requirements.txt
CHANGED
@@ -3,4 +3,5 @@ uvicorn
|
|
3 |
huggingface_hub
|
4 |
pydantic
|
5 |
torch==2.0.0
|
6 |
-
nltk
|
|
|
|
3 |
huggingface_hub
|
4 |
pydantic
|
5 |
torch==2.0.0
|
6 |
+
nltk
|
7 |
+
transformers
|