Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
@@ -21,6 +21,10 @@ model_name = "microsoft/DialoGPT-small"
|
|
21 |
tokenizer = AutoTokenizer.from_pretrained(model_name, cache_dir=cache_dir)
|
22 |
model = AutoModelForCausalLM.from_pretrained(model_name, cache_dir=cache_dir)
|
23 |
|
|
|
|
|
|
|
|
|
24 |
# Set device
|
25 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
26 |
model.to(device)
|
@@ -40,21 +44,30 @@ app.add_middleware(
|
|
40 |
class Question(BaseModel):
|
41 |
question: str
|
42 |
|
43 |
-
SYSTEM_PROMPT = "You are a helpful, professional, and highly persuasive sales assistant
|
44 |
|
45 |
-
chat_history_ids = None
|
46 |
|
47 |
async def generate_response_chunks(prompt: str):
|
48 |
global chat_history_ids
|
49 |
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
if chat_history_ids is not None:
|
53 |
input_ids = torch.cat([chat_history_ids, new_input_ids], dim=-1)
|
|
|
|
|
|
|
|
|
54 |
else:
|
55 |
input_ids = new_input_ids
|
56 |
|
57 |
-
|
58 |
output_ids = model.generate(
|
59 |
input_ids,
|
60 |
attention_mask=attention_mask,
|
@@ -65,15 +78,20 @@ async def generate_response_chunks(prompt: str):
|
|
65 |
pad_token_id=tokenizer.eos_token_id
|
66 |
)
|
67 |
|
|
|
|
|
68 |
|
69 |
-
|
70 |
-
|
71 |
response = tokenizer.decode(output_ids[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
|
72 |
|
|
|
73 |
for word in response.split():
|
74 |
yield word + " "
|
75 |
await asyncio.sleep(0.03)
|
76 |
|
77 |
@app.post("/ask")
|
78 |
async def ask(question: Question):
|
79 |
-
return StreamingResponse(
|
|
|
|
|
|
|
|
21 |
tokenizer = AutoTokenizer.from_pretrained(model_name, cache_dir=cache_dir)
|
22 |
model = AutoModelForCausalLM.from_pretrained(model_name, cache_dir=cache_dir)
|
23 |
|
24 |
+
# Set pad token if not defined
|
25 |
+
if tokenizer.pad_token is None:
|
26 |
+
tokenizer.pad_token = tokenizer.eos_token
|
27 |
+
|
28 |
# Set device
|
29 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
30 |
model.to(device)
|
|
|
44 |
class Question(BaseModel):
|
45 |
question: str
|
46 |
|
47 |
+
SYSTEM_PROMPT = "You are a helpful, professional, and highly persuasive sales assistant..."
|
48 |
|
49 |
+
chat_history_ids = None
|
50 |
|
51 |
async def generate_response_chunks(prompt: str):
|
52 |
global chat_history_ids
|
53 |
|
54 |
+
# Combine system prompt and user input
|
55 |
+
input_text = SYSTEM_PROMPT + "\nUser: " + prompt + "\nBot:"
|
56 |
+
new_input_ids = tokenizer.encode(input_text, return_tensors='pt').to(device)
|
57 |
+
|
58 |
+
# Create attention mask (handle case where pad_token_id might be None)
|
59 |
+
attention_mask = torch.ones_like(new_input_ids)
|
60 |
|
61 |
if chat_history_ids is not None:
|
62 |
input_ids = torch.cat([chat_history_ids, new_input_ids], dim=-1)
|
63 |
+
attention_mask = torch.cat([
|
64 |
+
torch.ones_like(chat_history_ids),
|
65 |
+
attention_mask
|
66 |
+
], dim=-1)
|
67 |
else:
|
68 |
input_ids = new_input_ids
|
69 |
|
70 |
+
# Generate response
|
71 |
output_ids = model.generate(
|
72 |
input_ids,
|
73 |
attention_mask=attention_mask,
|
|
|
78 |
pad_token_id=tokenizer.eos_token_id
|
79 |
)
|
80 |
|
81 |
+
# Update chat history
|
82 |
+
chat_history_ids = output_ids
|
83 |
|
84 |
+
# Decode only the new tokens
|
|
|
85 |
response = tokenizer.decode(output_ids[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
|
86 |
|
87 |
+
# Stream the response
|
88 |
for word in response.split():
|
89 |
yield word + " "
|
90 |
await asyncio.sleep(0.03)
|
91 |
|
92 |
@app.post("/ask")
|
93 |
async def ask(question: Question):
|
94 |
+
return StreamingResponse(
|
95 |
+
generate_response_chunks(question.question),
|
96 |
+
media_type="text/plain"
|
97 |
+
)
|