Spaces:
Sleeping
Sleeping
from fastapi import FastAPI | |
from fastapi.responses import StreamingResponse | |
from pydantic import BaseModel | |
from huggingface_hub import InferenceClient | |
import uvicorn | |
from typing import Generator | |
import json | |
import nltk | |
import os | |
from transformers import pipeline | |
# Set up the environment for NLTK | |
nltk.data.path.append(os.getenv('NLTK_DATA')) | |
# Initialize the FastAPI app | |
app = FastAPI() | |
# Initialize the InferenceClient with your model | |
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.2") | |
# Initialize the summarization pipeline | |
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6") | |
class Item(BaseModel): | |
prompt: str | |
history: list | |
system_prompt: str | |
temperature: float = 0.8 | |
max_new_tokens: int = 12000 | |
top_p: float = 0.15 | |
repetition_penalty: float = 1.0 | |
def summarize_history(history): | |
# Concatenate all history entries into a single string | |
full_history = " ".join(entry['content'] for entry in history if entry['role'] == 'user') | |
# Summarize the history | |
summarized_history = summarizer(full_history, max_length=1024, truncation=True) | |
return summarized_history[0]['summary_text'] | |
def format_prompt(current_prompt, history): | |
formatted_history = "<s>" | |
formatted_history += f"[HISTORY] {history} [/HISTORY]" | |
formatted_history += f"[USER] {current_prompt} [/USER]</s>" | |
return formatted_history | |
def generate_stream(item: Item) -> Generator[bytes, None, None]: | |
summarized_history = summarize_history(item.history) | |
formatted_prompt = format_prompt(item.prompt, summarized_history) | |
input_token_count = len(nltk.word_tokenize(formatted_prompt)) | |
max_tokens_allowed = 32768 | |
max_new_tokens_adjusted = max(1, min(item.max_new_tokens, max_tokens_allowed - input_token_count)) | |
generate_kwargs = { | |
"temperature": item.temperature, | |
"max_new_tokens": max_new_tokens_adjusted, | |
"top_p": item.top_p, | |
"repetition_penalty": item.repetition_penalty, | |
"do_sample": True, | |
"seed": 42, | |
} | |
for response in client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True): | |
chunk = { | |
"text": response.token.text, | |
"complete": response.generated_text is not None | |
} | |
yield json.dumps(chunk).encode("utf-8") + b"\n" | |
async def generate_text(item: Item): | |
return StreamingResponse(generate_stream(item), media_type="application/x-ndjson") | |
if __name__ == "__main__": | |
uvicorn.run(app, host="0.0.0.0", port=8000) | |