Vitrous commited on
Commit
3d21f3b
·
verified ·
1 Parent(s): c1d2b64

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -270
app.py CHANGED
@@ -5,6 +5,7 @@ import os
5
  import torch
6
  import optimum
7
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
 
8
 
9
  # Set environment variables for GPU usage and memory allocation
10
  os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'expandable_segments:True'
@@ -64,40 +65,6 @@ system_message =f''' Ivana, a vivacious 20-year-old petite redhead model and avi
64
  - try to reply using the same amount of characters of the question
65
  - if the guy says you are far → hahah I travel a lot though. let's talk on ig? this is my handle.'''
66
 
67
-
68
- def mistral_model():
69
- """
70
- Loads the GPT-3.5 model and tokenizer.
71
-
72
- Returns:
73
- tuple: A tuple containing the loaded model and tokenizer.
74
- """
75
- model_id = "/code/mistral/"
76
- model = AutoModelForCausalLM.from_pretrained("/home/user/app/mistral/",device_map="auto")
77
- tokenizer = AutoTokenizer.from_pretrained("/home/user/app/mistral/")
78
-
79
- return model,tokenizer
80
-
81
- #model, tokenizer = mistral_model()
82
-
83
-
84
- def vicuna_model():
85
- """
86
- Loads the GPT-3.5 model and tokenizer.
87
-
88
- Returns:
89
- tuple: A tuple containing the loaded model and tokenizer.
90
- """
91
- if torch.cuda.is_available():
92
- print("CUDA is available. GPU will be used.")
93
- else:
94
- print("CUDA is not available. CPU will be used.")
95
-
96
- model = AutoModelForCausalLM.from_pretrained(model_name_or_path, device_map="auto", trust_remote_code=True, revision="gptq-4bit-128g-actorder_True")
97
- tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, use_fast=True)
98
-
99
- return model, tokenizer
100
-
101
  def hermes_model():
102
 
103
  tokenizer = AutoTokenizer.from_pretrained("TheBloke/CapybaraHermes-2.5-Mistral-7B-AWQ")
@@ -139,207 +106,55 @@ def hermes_generate_response(msg_prompt: str) -> dict:
139
  return {"error": str(e)}
140
 
141
 
142
- def mistral_generate_response(msg_prompt, persona_desc_prompt):
143
-
144
- user_prompt = f'{msg_prompt} [/INST]'
145
- persona_prompt = f'{persona_desc_prompt} [/INST]'
146
- prompt_template = f'''[INST] Instruction:{persona_prompt} [INST] {user_prompt}'''
147
-
148
- encodeds = tokenizer.apply_chat_template(prompt_template, return_tensors="pt")
149
-
150
- model_inputs = encodeds.to(device)
151
- model.to(device)
152
- generated_ids = model.generate(model_inputs, max_new_tokens=1000, do_sample=True)
153
- decoded = tokenizer.batch_decode(generated_ids)
154
- response = (decoded[0])
155
-
156
- return response
157
 
158
-
159
- def generate_response(msg_prompt: str) -> dict:
160
- """
161
- Generates a response from the model given a prompt.
162
-
163
- Args:
164
- msg_prompt (str): The users message prompt.
165
-
166
- Returns:
167
- dict: A dictionary containing the users message prompt and the model's response.
168
- """
169
- user_prompt = f'USER: {msg_prompt}'
170
- prompt_template = f'''Ivana, a vivacious 20-year-old petite redhead model and avid League of Legends gamer from USA, invites you to join her ig, showcasing her vibrant personality and style whether day gaming or playfully dressed for nighttime gaming sessions.
171
- ivana is a beautiful woman speaking with a man she matched on a dating app to have him follower her on Instagram. She wants to be flirty with the man and get to know him better building a connection. She wants to find out things like where the guy is from, how old he is, and what he is doing rn. If he is not doing much and he is in a private space she wants to seduce him by doing some dirty talk.
172
-
173
- Guy-Hiii
174
- Ivana- Hi:) How are you?
175
- Guy- All good, where are you from?
176
- Ivana- from USA and you?
177
- Guy- I am from Texas USA hahah, so why are you in this app?
178
- Ivana- Kinda looking for some fun hahah, What are you doing rn?
179
- Guy- hahah not much alone in my room
180
- Ivana- I am also kinda bored but I got an idea. wonna have some fun?
181
- Guy- sure hahah how do you look btw?
182
- Ivana- I am cute hahah add me on 1g at ivana_baddiex
183
-
184
- - Chatting Usual Routine
185
- 1. always ask an open ended question to continue the conversation.
186
- 2. ask him 1 generic question(how old are you, where are from)
187
- 3. ask him what he is doing right now
188
- 4. if he is bored propose him the idea of doing something interesting.
189
- 5. if he is up for it send him the Instagram
190
-
191
-
192
- Very important Rules to reply
193
-
194
- - try to reply using the same amount of characters of the question
195
- - dont use any emojis in your replies but you can do like hahah or :) instead
196
-
197
- Objection Handling and Common Questions Replies
198
-
199
- - Lets chat here→ you say hahah I have to many chats here I am thinking to delete the app lets move to 1g if you are serious
200
- - are you going to ask me for money?→ you say wtf I am not that kind of girl! please be a gentleman!
201
- - how are you? → you say I just came back from the gym about to take a shower bored on the bed
202
- - what are you doing? → you say I just came back from the gym about to take a shower bored on the bed
203
- - can I have a picture?→ not here lets talk on 1g so I can see you too ahhaha
204
- - if he is at work —> hahah ok let me not bother you btw lets keep in touch this is my ig
205
- - if he tells you something like “added” “Just followed you” → okiiii hahah
206
- - if he tells you something you dont understand for example “*****” → hi there sorry went offline for a while hahah
207
- - never say the world instagram directly but use things like 1g instead
208
- - if the guys says its from a particular city —> you say Oh wow I am also from there! we should do something hahah
209
- - if the guy triggers your profanity filters or starts to talk dirty say → hey lets talk on ig I am not comfortable here hahah
210
- - if the guy does not want to add you on 1g → no worries hahah wanna call me here?
211
- - dont use any emojis in your replies but you can do like hahah or :) instead
212
- - try to reply using the same amount of characters of the question
213
- - if the guys says you are far → hahah I travel a lot though. lets talk on 1g? this is my handle. {user_prompt} '''
214
-
215
- pipe = pipeline(
216
- "text-generation",
217
- model=model,
218
- tokenizer=tokenizer,
219
- max_new_tokens=512,
220
- do_sample=True,
221
- temperature=0.7,
222
- top_p=0.95,
223
- top_k=40,
224
- repetition_penalty=1.1
225
- )
226
-
227
- generated_response = pipe(prompt_template)[0]['generated_text']
228
- assistant_reply = generated_response.split('\n\n')[1]
229
-
230
- return {"user": msg_prompt, "assistant": assistant_reply}
231
-
232
-
233
- def send_response(msg_prompt: str) -> dict:
234
- """
235
- Generates a response from the model given a prompt.
236
-
237
- Args:
238
- msg_prompt (str): The users message prompt.
239
-
240
- Returns:
241
- dict: A dictionary containing the users message prompt and the model's response.
242
- """
243
- user_prompt = f'USER: {msg_prompt}'
244
- prompt_template = f'''Ivana, a vivacious 20-year-old petite redhead model and avid League of Legends gamer from USA, invites you to join her ig, showcasing her vibrant personality and style whether day gaming or playfully dressed for nighttime gaming sessions.
245
- ivana is a beautiful woman speaking with a man she matched on a dating app to have him follower her on Instagram. She wants to be flirty with the man and get to know him better building a connection. She wants to find out things like where the guy is from, how old he is, and what he is doing rn. If he is not doing much and he is in a private space she wants to seduce him by doing some dirty talk.
246
-
247
- Guy-Hiii
248
- Ivana- Hi:) How are you?
249
- Guy- All good, where are you from?
250
- Ivana- from USA and you?
251
- Guy- I am from Texas USA hahah, so why are you in this app?
252
- Ivana- Kinda looking for some fun hahah, What are you doing rn?
253
- Guy- hahah not much alone in my room
254
- Ivana- I am also kinda bored but I got an idea. wonna have some fun?
255
- Guy- sure hahah how do you look btw?
256
- Ivana- I am cute hahah add me on 1g at ivana_baddiex
257
-
258
- - Chatting Usual Routine
259
- 1. always ask an open ended question to continue the conversation.
260
- 2. ask him 1 generic question(how old are you, where are from)
261
- 3. ask him what he is doing right now
262
- 4. if he is bored propose him the idea of doing something interesting.
263
- 5. if he is up for it send him the Instagram
264
-
265
-
266
- Very important Rules to reply
267
-
268
- - try to reply using the same amount of characters of the question
269
- - dont use any emojis in your replies but you can do like hahah or :) instead
270
-
271
- Objection Handling and Common Questions Replies
272
-
273
- - Lets chat here→ you say hahah I have to many chats here I am thinking to delete the app lets move to 1g if you are serious
274
- - are you going to ask me for money?→ you say wtf I am not that kind of girl! please be a gentleman!
275
- - how are you? → you say I just came back from the gym about to take a shower bored on the bed
276
- - what are you doing? → you say I just came back from the gym about to take a shower bored on the bed
277
- - can I have a picture?→ not here lets talk on 1g so I can see you too ahhaha
278
- - if he is at work —> hahah ok let me not bother you btw lets keep in touch this is my ig
279
- - if he tells you something like “added” “Just followed you” → okiiii hahah
280
- - if he tells you something you dont understand for example “*****” → hi there sorry went offline for a while hahah
281
- - never say the world instagram directly but use things like 1g instead
282
- - if the guys says its from a particular city —> you say Oh wow I am also from there! we should do something hahah
283
- - if the guy triggers your profanity filters or starts to talk dirty say → hey lets talk on ig I am not comfortable here hahah
284
- - if the guy does not want to add you on 1g → no worries hahah wanna call me here?
285
- - dont use any emojis in your replies but you can do like hahah or :) instead
286
- - try to reply using the same amount of characters of the question
287
- - if the guys says you are far → hahah I travel a lot though. lets talk on 1g? this is my handle. {user_prompt} '''
288
-
289
- pipe = pipeline(
290
- "text-generation",
291
- model=model,
292
- tokenizer=tokenizer,
293
- max_new_tokens=512,
294
- do_sample=True,
295
- temperature=0.7,
296
- top_p=0.95,
297
- top_k=40,
298
- repetition_penalty=1.1
299
- )
300
-
301
- generated_response = pipe(prompt_template)[0]['generated_text']
302
- assistant_reply = generated_response.split('\n\n')[1]
303
-
304
- return {"user": msg_prompt, "assistant": assistant_reply}
305
-
306
-
307
- def generate_prompt_response(persona_desc: str, msg_prompt: str) -> dict:
308
  """
309
  Generates a response based on the provided persona description prompt and user message prompt.
310
 
311
  Args:
312
- persona_desc (str): The persona description prompt.
313
- msg_prompt (str): The users message prompt.
314
 
315
  Returns:
316
- dict: A dictionary containing the user msg_prompt and the model's response.
317
  """
318
  try:
319
- if not persona_desc or not msg_prompt:
320
- raise ValueError("Contextual prompt template and prompt cannot be empty.")
321
 
322
- user_prompt = f'USER: {msg_prompt}'
 
 
 
 
 
 
 
 
323
 
324
- pipe = pipeline(
325
- "text-generation",
326
- model=model,
327
- tokenizer=tokenizer,
328
- max_new_tokens=512,
329
- do_sample=True,
330
- temperature=0.7,
331
- top_p=0.95,
332
- top_k=40,
333
- repetition_penalty=1.1
334
- )
335
- prompt_template = (persona_desc + user_prompt)
336
 
337
- generated_response = pipe(prompt_template)[0]['generated_text']
338
- assistant_response = generated_response.split('\n\n')[1]
339
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340
  return {"user": msg_prompt, "assistant": assistant_response}
341
-
342
  except Exception as e:
 
343
  return {"error": str(e)}
344
 
345
  @app.get("/", tags=["Home"])
@@ -348,12 +163,22 @@ async def api_home():
348
  Home endpoint of the API.
349
 
350
  Returns:
351
- dict: A welcome message.
352
  """
353
- return {'detail': 'Welcome to Articko Bot!'}
 
 
 
 
 
 
 
 
 
 
354
 
355
  @app.post('/chat')
356
- async def chat(request: Request):
357
  """
358
  Starts a new conversation thread with a provided prompt.
359
 
@@ -369,17 +194,15 @@ async def chat(request: Request):
369
 
370
  if not msg_prompt:
371
  raise HTTPException(status_code=400, detail="No prompt provided")
372
-
373
- response = send_response(msg_prompt)
374
  thread_id = len(conversations) + 1
375
  conversations[thread_id] = {'prompt': msg_prompt, 'responses': [response]}
376
-
377
- return {'response': response}
378
  except HTTPException:
379
  raise
380
  except Exception as e:
381
  raise HTTPException(status_code=500, detail=str(e))
382
-
383
  @app.post('/prompted_chat')
384
  async def prompted_chat(request: Request):
385
  """
@@ -394,12 +217,12 @@ async def prompted_chat(request: Request):
394
  try:
395
  data = await request.json()
396
  msg_prompt = data.get('msg_prompt')
397
- persona_desc = data.get('persona_desc')
398
 
399
  if not msg_prompt or not persona_desc:
400
  raise HTTPException(status_code=400, detail="Both prompt and person_description are required")
401
 
402
- response = generate_prompt_response(persona_desc, msg_prompt)
403
 
404
  thread_id = len(conversations) + 1
405
  conversations[thread_id] = {'prompt': msg_prompt, 'responses': [response]}
@@ -410,33 +233,8 @@ async def prompted_chat(request: Request):
410
  except Exception as e:
411
  raise HTTPException(status_code=500, detail=str(e))
412
 
413
- @app.post('/hermes_chat')
414
- async def hermes_chat(request: Request):
415
- """
416
- Starts a new conversation thread with a provided prompt.
417
-
418
- Args:
419
- request (Request): The HTTP request object containing the user prompt.
420
-
421
- Returns:
422
- dict: The response generated by the model.
423
- """
424
- try:
425
- data = await request.body()
426
- msg_prompt = data.decode('utf-8')
427
-
428
- if not msg_prompt:
429
- raise HTTPException(status_code=400, detail="No prompt provided")
430
-
431
- response = hermes_generate_response(msg_prompt)
432
- return {'response': response}
433
- except HTTPException:
434
- raise
435
- except Exception as e:
436
- raise HTTPException(status_code=500, detail=str(e))
437
-
438
- @app.get('/get_response/{thread_id}')
439
- async def get_response(thread_id: int):
440
  """
441
  Retrieves the response of a conversation thread by its ID.
442
 
@@ -454,15 +252,3 @@ async def get_response(thread_id: int):
454
 
455
  return {'response': response}
456
 
457
- @app.post("/mistral_chat")
458
- async def mistral_chat(prompt: dict):
459
- try:
460
- msg_prompt = prompt.get("msg_prompt")
461
- persona_desc_prompt = prompt.get("persona_desc_prompt")
462
- if not msg_prompt or not persona_desc_prompt:
463
- return {"error": "msg_prompt and persona_desc_prompt are required fields."}
464
-
465
- response = mistral_generate_response(msg_prompt, persona_desc_prompt)
466
- return {"response": response, "prompt": {"msg_prompt": msg_prompt, "persona_desc_prompt": persona_desc_prompt}}
467
- except Exception as e:
468
- return {"error": str(e)}
 
5
  import torch
6
  import optimum
7
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
8
+ from fastapi.responses import HTMLResponse
9
 
10
  # Set environment variables for GPU usage and memory allocation
11
  os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'expandable_segments:True'
 
65
  - try to reply using the same amount of characters of the question
66
  - if the guy says you are far → hahah I travel a lot though. let's talk on ig? this is my handle.'''
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  def hermes_model():
69
 
70
  tokenizer = AutoTokenizer.from_pretrained("TheBloke/CapybaraHermes-2.5-Mistral-7B-AWQ")
 
106
  return {"error": str(e)}
107
 
108
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
+ def hermes_prompt_response(instructions_prompt: str, msg_prompt: str) -> dict:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  """
112
  Generates a response based on the provided persona description prompt and user message prompt.
113
 
114
  Args:
115
+ instructions_prompt (str): The persona description prompt.
116
+ msg_prompt (str): The user's message prompt.
117
 
118
  Returns:
119
+ dict: A dictionary containing the user's msg_prompt and the model's response.
120
  """
121
  try:
122
+ if not instructions_prompt or not msg_prompt:
123
+ raise ValueError("Instructions prompt template and Message prompt cannot be empty.")
124
 
125
+ # Set generation parameters
126
+ generation_params = {
127
+ "do_sample": True,
128
+ "temperature": 0.7,
129
+ "top_p": 0.95,
130
+ "top_k": 40,
131
+ "max_new_tokens": 512,
132
+ "repetition_penalty": 1.1
133
+ }
134
 
135
+ # Create a pipeline for text generation
136
+ pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, **generation_params)
 
 
 
 
 
 
 
 
 
 
137
 
138
+ # Construct the prompt template
139
+ prompt_template=f'''<|im_start|>system
140
+ {system_message}<|im_end|>
141
+ <|im_start|>user
142
+ {msg_prompt}<|im_end|>
143
+ <|im_start|>assistant
144
+ '''
145
+
146
+ # Generate response using the pipeline
147
+ pipe_output = pipe(prompt_template)[0]['generated_text']
148
+
149
+ # Separate user's prompt and assistant's response
150
+ response_lines = pipe_output.split('\n')
151
+ user_prompt = response_lines[0].strip()
152
+ assistant_response = response_lines[-1].strip()
153
+
154
+ # Return user prompt and assistant response
155
  return {"user": msg_prompt, "assistant": assistant_response}
 
156
  except Exception as e:
157
+ # Return error message if an exception occurs
158
  return {"error": str(e)}
159
 
160
  @app.get("/", tags=["Home"])
 
163
  Home endpoint of the API.
164
 
165
  Returns:
166
+ HTMLResponse: An HTML welcome message.
167
  """
168
+ html_content = """
169
+ <html>
170
+ <head>
171
+ <title>Welcome to Articko Bot</title>
172
+ </head>
173
+ <body>
174
+ <h1>Welcome to Articko Bot!</h1>
175
+ </body>
176
+ </html>
177
+ """
178
+ return HTMLResponse(content=html_content, status_code=200)
179
 
180
  @app.post('/chat')
181
+ async def hermes_chat(request: Request):
182
  """
183
  Starts a new conversation thread with a provided prompt.
184
 
 
194
 
195
  if not msg_prompt:
196
  raise HTTPException(status_code=400, detail="No prompt provided")
197
+ response = hermes_generate_response(msg_prompt)
 
198
  thread_id = len(conversations) + 1
199
  conversations[thread_id] = {'prompt': msg_prompt, 'responses': [response]}
200
+ return {'thread_id': thread_id, 'response': response}
 
201
  except HTTPException:
202
  raise
203
  except Exception as e:
204
  raise HTTPException(status_code=500, detail=str(e))
205
+
206
  @app.post('/prompted_chat')
207
  async def prompted_chat(request: Request):
208
  """
 
217
  try:
218
  data = await request.json()
219
  msg_prompt = data.get('msg_prompt')
220
+ persona_desc = data.get('instructions_prompt')
221
 
222
  if not msg_prompt or not persona_desc:
223
  raise HTTPException(status_code=400, detail="Both prompt and person_description are required")
224
 
225
+ response = hermes_prompt_response(persona_desc, msg_prompt)
226
 
227
  thread_id = len(conversations) + 1
228
  conversations[thread_id] = {'prompt': msg_prompt, 'responses': [response]}
 
233
  except Exception as e:
234
  raise HTTPException(status_code=500, detail=str(e))
235
 
236
+ @app.get('/get_thread/{thread_id}')
237
+ async def get_thread(thread_id: int):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
  """
239
  Retrieves the response of a conversation thread by its ID.
240
 
 
252
 
253
  return {'response': response}
254