Dooratre commited on
Commit
53569f4
1 Parent(s): f598023

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -70
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from flask import Flask, render_template, request, send_from_directory, url_for, flash,redirect,session
2
  import requests
3
  import base64
4
  import json
@@ -14,9 +14,9 @@ app = Flask(__name__)
14
  app.secret_key = 'wefwefwef'
15
  app.config['UPLOAD_FOLDER'] = 'uploads'
16
 
17
- def get_assistant_response(user_input):
18
 
19
- message = f"Follow this Role in our chat : \n \n SYSTEM PROMPT : you are AI model Nmaed ALEX-9 created By Libyan team your task is help users as you can \n \n ABOUT MODEL : the ALEX-9 model is not normal model as Chatgpt , the ALEX-9 model can chat and create images , photos you can called him AI image and chat generation , \n the model is so helpful , Nicely , Smart , Use Emojeis and Fun words , \n \n and for Developers Users will be so helpful because the model was training about Big DATASET of coding with alot of programming lang but he is not good in desine coding Like HTML , CSS etc of desine let's say the model is so perfect in the Backend and you are not from Open ai you are created and developed by Libyan team Start chat with this user \n \n old chat : {conversation_history} \n \n USERNAME : {user_name} : {user_input} \n \n ALEX-9:"
20
  payload = {
21
  "identifier_type": "object_id",
22
  "identifier_value": "4775260",
@@ -49,28 +49,40 @@ def get_assistant_response(user_input):
49
 
50
  return combined_content
51
 
52
- def get_image_Ai(user_input):
53
 
 
54
  payload = {
55
- "mode": "chat",
56
- "chat_history":"" ,
57
- "data": {
58
- "query": f"you are AI for chat and you can create generate images by use this ~~ here Example : {images} \n \n here the old chat : {image_chat} \n \n user : {user_input} \n \n So your response to user will be : ",
59
- "loader": "PDFReader",
60
- "text":""
61
- }
 
 
62
  }
 
63
 
64
- response = requests.post(url3, headers=headers, json=payload)
65
- data = response.json()
66
-
67
- # Extract the response from the data
68
- response_text = data["data"]["response"]
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
- response_text = response_text.strip().replace('\n', '<br>')
72
 
73
- return response_text
74
 
75
 
76
 
@@ -84,7 +96,7 @@ def generate_image(prompt):
84
 
85
  payload = {
86
  "json": True,
87
- "prompt": f"{prompt} Realastic Photo 4K",
88
  "public_topic": "/studios/516104/wsuid_new-edge-4_nodeid_editor-4/imageGen/1719250632789"
89
  }
90
 
@@ -111,7 +123,6 @@ headers2 = {
111
  with open('image.json', 'r') as file:
112
  images = file.read()
113
 
114
- image_chat = []
115
  conversation_history = []
116
  user_name = []
117
 
@@ -213,7 +224,7 @@ def process_account_action(action, account_username, password, first_name=None,
213
  if upload_image_to_github(upload_image, image_github_path):
214
  existing_accounts.append(new_account)
215
  else:
216
- return "Failed to upload image to GitHub."
217
 
218
  updated_content = base64.b64encode(json.dumps(existing_accounts).encode()).decode()
219
 
@@ -230,7 +241,7 @@ def process_account_action(action, account_username, password, first_name=None,
230
  response = requests.put(content_url, json=data, headers=headers)
231
 
232
  if response.status_code == 200:
233
- return "Account created successfully."
234
  else:
235
  return f"Failed to create account. Status Code: {response.status_code}, Response: {response.json()}"
236
  elif action == "login":
@@ -315,34 +326,21 @@ def create_account():
315
 
316
  return render_template('sign-up.html')
317
 
318
- @app.route("/")
319
- def home():
320
- global conversation_history
321
- global image_chat
322
- global user_name
323
- conversation_history = []
324
- image_chat = []
325
- user_name = []
326
-
327
- # Retrieve user information from the session
328
- user_info = session.get('user_info', {})
329
- account_name = user_info.get('account_name')
330
- first = user_info.get('first')
331
- user_name.append(first)
332
- last = user_info.get('last')
333
- token2 = user_info.get('token2')
334
- greet = user_info.get('greet')
335
-
336
- if not account_name:
337
 
338
- return redirect(url_for('login'))
 
 
 
 
339
 
340
  return render_template('ai-chat-bot.html', account_name=account_name, greet=greet, first=first, last=last, token2=token2)
341
 
342
 
343
 
344
-
345
-
346
  @app.route("/login", methods=["GET", "POST"])
347
  def login():
348
  message = None
@@ -380,42 +378,36 @@ def login():
380
  ############## END INFO ABOUT USER #################################################
381
 
382
  # Initialize the user input and get the assistant's response
 
383
  user_input = f"user name : {first} \n \n message : أهلا"
384
- greet = get_assistant_response(user_input)
385
-
386
- # Store user details in the session
387
- session['user_info'] = {
388
- 'account_name': account_name,
389
- 'first': first,
390
- 'last': last,
391
- 'token2': token2,
392
- 'greet': greet
393
- }
394
 
395
- # Redirect to the main route
396
- return redirect(url_for('home'))
 
 
 
 
397
 
 
 
 
398
  return render_template("sign-in.html", message=message)
399
 
400
-
401
-
402
  ############## END LOGIN #############################################
403
 
404
  @app.route('/get_response', methods=['POST'])
405
  def get_response():
406
  user_input = request.form['user_input']
407
- conversation_history.append({f"{user_name} : {user_input}"})
408
- image_chat.append({f"user : {user_input}"})
409
 
410
  if user_input.lower() == "exit":
411
  return "exit"
412
 
413
- response_text = get_assistant_response(user_input)
414
- check_image = get_image_Ai(user_input)
415
 
416
  if "~" in check_image:
417
- image_chat.append({f"AI : {check_image}"})
418
- conversation_history.append({f"AI : {check_image}"})
419
  prompt_start = check_image.index("~") + 1
420
  prompt_end = check_image.index("~", prompt_start)
421
  prompt = check_image[prompt_start:prompt_end]
@@ -423,13 +415,9 @@ def get_response():
423
  # Call the text-to-image API
424
  image_url = generate_image(prompt)
425
  check_image += f"<br><br><img src='{image_url}'>"
426
- # Delete the prompt from the response text
427
  check_image = check_image.replace("~" + prompt + "~", "")
428
  response_text = check_image
429
- else :
430
- image_chat.append({f"\n AI : {response_text}"})
431
- conversation_history.append({f"AI : {response_text}"})
432
-
433
 
434
  return response_text
435
 
@@ -442,5 +430,9 @@ def send_static(path):
442
  def send_next_static(path):
443
  return send_from_directory('_next', path)
444
 
 
 
 
 
445
  if __name__ == '__main__':
446
- app.run(host="0.0.0.0", port=7860)
 
1
+ from flask import Flask, render_template, request, send_from_directory, url_for, flash,redirect , jsonify, session, flash
2
  import requests
3
  import base64
4
  import json
 
14
  app.secret_key = 'wefwefwef'
15
  app.config['UPLOAD_FOLDER'] = 'uploads'
16
 
17
+ def get_assistant_response(user_input , conversation_history):
18
 
19
+ message = f"Follow this Role in our chat : \n \n SYSTEM PROMPT : you are AI model Nmaed ALEX-9 created By Libyan team your task is help users as you can \n \n ABOUT MODEL : the ALEX-9 model is not normal model as Chatgpt , the ALEX-9 model can chat and create images , photos you can called him AI image and chat generation , \n the model is so helpful , Nicely , Smart , Use Emojeis and Fun words , \n \n and for Developers Users will be so helpful because the model was training about Big DATASET of coding with alot of programming lang perfect in the Backend and you are not from Open ai you are created and developed by Libyan team Start chat with this user\n the model is so helpful , Nicely , Smart , Use Emojeis and Fun words , \n \n chat start now : {conversation_history}\n \n USERNAME : : {user_input} \n \n ALEX-9:"
20
  payload = {
21
  "identifier_type": "object_id",
22
  "identifier_value": "4775260",
 
49
 
50
  return combined_content
51
 
52
+ def get_image_Ai(user_input , conversation_history):
53
 
54
+ message = f"you are AI for chat and you can create generate images by use this ~~ here Example : {images} \n \n here the chat : \n{conversation_history} \n \n {user_input} \n \n \n So your response to user will be : "
55
  payload = {
56
+ "identifier_type": "object_id",
57
+ "identifier_value": "4775260",
58
+ "pubsub_topic": "/studios/516104/wsuid_4647155_new-edge-112_nodeid_editor-111/textGenStream/1722445975926",
59
+ "stream": True,
60
+
61
+ "variables": {
62
+ "Input": f"{message}",
63
+ "User Input": f"{message}",
64
+ "input": f"{message}",
65
  }
66
+ }
67
 
 
 
 
 
 
68
 
69
+ response = requests.post(url2, headers=headers, json=payload)
70
+ combined_content = ""
71
+
72
+ for line in response.text.splitlines():
73
+ if line.startswith('data: '):
74
+ json_data = line[6:].strip()
75
+ try:
76
+ data = json.loads(json_data)
77
+ delta_content = data.get('choices')[0].get('delta').get('content', '')
78
+ combined_content += delta_content
79
+ except json.JSONDecodeError:
80
+ continue
81
+
82
+ combined_content = combined_content.replace("\n", "<br>")
83
 
 
84
 
85
+ return combined_content
86
 
87
 
88
 
 
96
 
97
  payload = {
98
  "json": True,
99
+ "prompt": f"{prompt}",
100
  "public_topic": "/studios/516104/wsuid_new-edge-4_nodeid_editor-4/imageGen/1719250632789"
101
  }
102
 
 
123
  with open('image.json', 'r') as file:
124
  images = file.read()
125
 
 
126
  conversation_history = []
127
  user_name = []
128
 
 
224
  if upload_image_to_github(upload_image, image_github_path):
225
  existing_accounts.append(new_account)
226
  else:
227
+ return "Failed to upload image to DataBase."
228
 
229
  updated_content = base64.b64encode(json.dumps(existing_accounts).encode()).decode()
230
 
 
241
  response = requests.put(content_url, json=data, headers=headers)
242
 
243
  if response.status_code == 200:
244
+ return "Account created successfully Please Sign In"
245
  else:
246
  return f"Failed to create account. Status Code: {response.status_code}, Response: {response.json()}"
247
  elif action == "login":
 
326
 
327
  return render_template('sign-up.html')
328
 
329
+ @app.route("/chat")
330
+ def chat():
331
+ if 'account_name' not in session or session['account_name'] is None:
332
+ return redirect(url_for('login'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333
 
334
+ account_name = session.get("account_name")
335
+ first = session.get("first")
336
+ last = session.get("last")
337
+ token2 = session.get("token2")
338
+ greet = session.get("greet")
339
 
340
  return render_template('ai-chat-bot.html', account_name=account_name, greet=greet, first=first, last=last, token2=token2)
341
 
342
 
343
 
 
 
344
  @app.route("/login", methods=["GET", "POST"])
345
  def login():
346
  message = None
 
378
  ############## END INFO ABOUT USER #################################################
379
 
380
  # Initialize the user input and get the assistant's response
381
+ conversation_history = {}
382
  user_input = f"user name : {first} \n \n message : أهلا"
383
+ greet = get_assistant_response(user_input , conversation_history )
 
 
 
 
 
 
 
 
 
384
 
385
+ # Store data in the session
386
+ session['account_name'] = account_name
387
+ session['first'] = first
388
+ session['last'] = last
389
+ session['token2'] = token2
390
+ session['greet'] = greet
391
 
392
+ # Redirect to the clear_history route
393
+ return redirect(url_for('chat'))
394
+
395
  return render_template("sign-in.html", message=message)
396
 
 
 
397
  ############## END LOGIN #############################################
398
 
399
  @app.route('/get_response', methods=['POST'])
400
  def get_response():
401
  user_input = request.form['user_input']
402
+ conversation_history = request.form.get('conversation_history', '[]') # Get the conversation history
 
403
 
404
  if user_input.lower() == "exit":
405
  return "exit"
406
 
407
+ response_text = get_assistant_response(user_input,conversation_history)
408
+ check_image = get_image_Ai(user_input,conversation_history)
409
 
410
  if "~" in check_image:
 
 
411
  prompt_start = check_image.index("~") + 1
412
  prompt_end = check_image.index("~", prompt_start)
413
  prompt = check_image[prompt_start:prompt_end]
 
415
  # Call the text-to-image API
416
  image_url = generate_image(prompt)
417
  check_image += f"<br><br><img src='{image_url}'>"
418
+ # Delete the prompt from the response text
419
  check_image = check_image.replace("~" + prompt + "~", "")
420
  response_text = check_image
 
 
 
 
421
 
422
  return response_text
423
 
 
430
  def send_next_static(path):
431
  return send_from_directory('_next', path)
432
 
433
+ @app.route('/svg/<path:path>')
434
+ def send_svg_static(path):
435
+ return send_from_directory('svg', path)
436
+
437
  if __name__ == '__main__':
438
+ app.run()