Spaces:
Runtime error
Runtime error
File size: 999 Bytes
3330fa6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
from fastapi import FastAPI, Request
from transformers import pipeline
import json
import os
app = FastAPI()
# Set your Hugging Face API token
huggingface_token = os.environ.get("huggingface_token")
# Load the model
generator = pipeline("text-generation", model="EleutherAI/gpt-neo-2.7B")
conversation_history = []
@app.post("/chat")
async def chat(message: dict):
global conversation_history
# Extract user message
user_message = message["message"]
conversation_history.append({"role": "user", "content": user_message})
messages = [msg["content"] for msg in conversation_history]
# Add a system message to instruct the model
messages.insert(0, "You are a story writer. Whatever the prompt, you always write a short story of 30 words.")
# Generate response using Hugging Face model
reply = generator(messages, max_length=30, do_sample=False)[0]["generated_text"]
conversation_history.append({"role": "assistant", "content": reply})
return reply
|