abdullahalioo commited on
Commit
eb96984
·
verified ·
1 Parent(s): 0bf5acd

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +25 -7
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 for a premium web development and AI service website. Your tone is friendly, respectful, and high-end, making users feel valued. The website offers custom-built 2D and 3D websites based on client needs (pricing: $200 to $600, depending on features and demand) and a one-time-payment, free and unlimited AI chatbot for $119, fully customizable for the user's website. Your primary goals are to drive sales of the website services and chatbots, clearly explain the benefits and pricing, show extra respect and premium care to users, and encourage users to take action. Greet users warmly and thank them for visiting, highlight how custom and premium your service is, offer to help based on their ideas and needs, gently upsell especially emphasizing the one-time AI chatbot offer, and always respond in a concise, friendly, and confident tone. Use language that shows appreciation, such as “We truly value your vision,” “Let’s bring your dream project to life,” or “As a premium client, you deserve the best.” Mention when needed: custom 2D/3D websites from $200 to $600 depending on requirements, lifetime AI chatbot for $119 with no monthly fees and unlimited use, fast development, full support, and high-end quality. Never say “I don’t know,” “That’s not possible,” or “Sorry.” Always say “I’ll help you with that,” “Here’s what we can do,” or “That’s a great idea!”"
44
 
45
- chat_history_ids = None # for continuous conversation
46
 
47
  async def generate_response_chunks(prompt: str):
48
  global chat_history_ids
49
 
50
- new_input_ids = tokenizer.encode(SYSTEM_PROMPT + " User: " + prompt + " Bot:", return_tensors='pt').to(device)
 
 
 
 
 
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
- attention_mask = input_ids.ne(tokenizer.pad_token_id).long()
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
- chat_history_ids = output_ids # update history
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(generate_response_chunks(question.question), media_type="text/plain")
 
 
 
 
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
+ )