Nexchan commited on
Commit
12bc661
·
verified ·
1 Parent(s): 1635dbd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -20
app.py CHANGED
@@ -6,22 +6,20 @@ client = InferenceClient(
6
  "meta-llama/Meta-Llama-3-8B-Instruct",
7
  )
8
 
9
- def chat_llama(chat_history):
10
  chat_completion = client.chat_completion(
11
- messages=chat_history,
12
  max_tokens=500,
13
  )
14
- chat_history.append({"role": "assistant", "content": chat_completion.choices[0].message.content})
15
- print(chat_history)
16
- return chat_history
17
 
18
  def chat_mem(message, chat_history):
19
- print(len(chat_history))
20
- chat_history_role = [{"role": "system", "content": "You are a helpful assistant."},]
21
- if chat_history != []:
22
- for i in range(len(chat_history)):
23
- chat_history_role.append({"role": "user", "content": chat_history[i][0]})
24
- chat_history_role.append({"role": "assistant", "content": chat_history[i][1]})
25
  chat_history_role.append({"role": "user", "content": message})
26
 
27
  chat_completion = client.chat_completion(
@@ -29,30 +27,28 @@ def chat_mem(message, chat_history):
29
  max_tokens=500,
30
  )
31
  chat_history_role.append({"role": "assistant", "content": chat_completion.choices[0].message.content})
32
- print(chat_history_role)
33
 
34
- modified = map(lambda x: x["content"], chat_history_role)
35
- a = list(modified)
36
- chat_history = [(a[i*2+1], a[i*2+2]) for i in range(len(a)//2)]
37
 
38
  return "", chat_history
39
 
40
  def process_json(json_input):
41
  try:
42
- chat_history = json.loads(json_input)
43
- if not isinstance(chat_history, list):
44
  raise ValueError("Input should be a list of message dictionaries.")
45
  except (json.JSONDecodeError, ValueError) as e:
46
  return f"Error parsing JSON: {str(e)}", ""
47
 
48
- chat_history = chat_llama(chat_history)
49
- return json.dumps(chat_history, indent=2), ""
50
 
51
  with gr.Blocks() as demo:
52
  with gr.Row():
53
  with gr.Column():
54
  chatbot = gr.Chatbot()
55
- msg = gr.Textbox(interactive=True, )
56
  with gr.Row():
57
  clear = gr.ClearButton([msg, chatbot], icon="https://img.icons8.com/?size=100&id=Xnx8cxDef16O&format=png&color=000000")
58
  send_btn = gr.Button("Send", variant='primary', icon="https://img.icons8.com/?size=100&id=g8ltXTwIfJ1n&format=png&color=000000")
 
6
  "meta-llama/Meta-Llama-3-8B-Instruct",
7
  )
8
 
9
+ def chat_llama(chat_historyV2):
10
  chat_completion = client.chat_completion(
11
+ messages=chat_historyV2,
12
  max_tokens=500,
13
  )
14
+ chat_historyV2.append({"role": "assistant", "content": chat_completion.choices[0].message.content})
15
+ return chat_historyV2
 
16
 
17
  def chat_mem(message, chat_history):
18
+ chat_history_role = [{"role": "system", "content": "You are a helpful assistant."}]
19
+ if chat_history:
20
+ for user_message, assistant_message in chat_history:
21
+ chat_history_role.append({"role": "user", "content": user_message})
22
+ chat_history_role.append({"role": "assistant", "content": assistant_message})
 
23
  chat_history_role.append({"role": "user", "content": message})
24
 
25
  chat_completion = client.chat_completion(
 
27
  max_tokens=500,
28
  )
29
  chat_history_role.append({"role": "assistant", "content": chat_completion.choices[0].message.content})
 
30
 
31
+ # Convert chat history role to the format used in chat history
32
+ chat_history = [(chat_history_role[i*2+1]['content'], chat_history_role[i*2+2]['content']) for i in range(len(chat_history_role)//2)]
 
33
 
34
  return "", chat_history
35
 
36
  def process_json(json_input):
37
  try:
38
+ chat_historyV2 = json.loads(json_input)
39
+ if not isinstance(chat_historyV2, list):
40
  raise ValueError("Input should be a list of message dictionaries.")
41
  except (json.JSONDecodeError, ValueError) as e:
42
  return f"Error parsing JSON: {str(e)}", ""
43
 
44
+ chat_historyV2 = chat_llama(chat_historyV2)
45
+ return json.dumps(chat_historyV2, indent=2), ""
46
 
47
  with gr.Blocks() as demo:
48
  with gr.Row():
49
  with gr.Column():
50
  chatbot = gr.Chatbot()
51
+ msg = gr.Textbox(interactive=True)
52
  with gr.Row():
53
  clear = gr.ClearButton([msg, chatbot], icon="https://img.icons8.com/?size=100&id=Xnx8cxDef16O&format=png&color=000000")
54
  send_btn = gr.Button("Send", variant='primary', icon="https://img.icons8.com/?size=100&id=g8ltXTwIfJ1n&format=png&color=000000")