erndgn commited on
Commit
37d4fd3
1 Parent(s): cf1096a

Refactor message handling, add undo/retry features

Browse files
Files changed (1) hide show
  1. app.py +65 -21
app.py CHANGED
@@ -3,6 +3,7 @@ import requests
3
  import json
4
  import os
5
  import uuid
 
6
 
7
  ENDPOINT = os.environ.get("ENDPOINT", None)
8
  TOKEN = os.environ.get("TOKEN", None)
@@ -33,10 +34,7 @@ def convert_to_messages(message, history):
33
 
34
  return messages
35
 
36
- def chat_cosmosllama(message: str,
37
- history: list,
38
- conversation_id: str
39
- ) -> str:
40
  messages = convert_to_messages(message, history)
41
 
42
  headers = {
@@ -44,52 +42,98 @@ def chat_cosmosllama(message: str,
44
  "Content-Type": "application/json"
45
  }
46
 
47
- response = requests.post(f"https://model.cosmosytu.online{ENDPOINT}", json={"messages": messages, "conversation_id": conversation_id}, headers=headers, stream=True)
 
 
 
 
 
 
 
 
 
48
 
49
  if response.status_code == 200:
50
- full_response = ""
51
  for line in response.iter_lines():
52
  if line:
53
  decoded_line = line.decode('utf-8')
54
  try:
55
  json_line = json.loads(decoded_line[6:])
56
 
 
 
 
57
  if "CosmosLlama" in json_line:
58
  full_response += json_line['CosmosLlama']
59
- yield full_response
60
- except Exception as e:
61
- print(e)
62
- pass
63
  else:
64
- yield "Bir hata oluştu."
65
 
66
  def get_conversation_id():
67
  return str(uuid.uuid4())
68
 
69
- def reset_conversation():
70
- new_conversation_id = get_conversation_id()
71
- return new_conversation_id
72
 
73
- chatbot=gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Cosmos LLaMa')
74
 
75
  with gr.Blocks(fill_height=True, css=css) as demo:
 
 
76
 
77
- conversation_id_state = gr.State(value=get_conversation_id)
78
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  interface = gr.ChatInterface(
80
- fn=chat_cosmosllama,
81
  chatbot=chatbot,
82
  fill_height=True,
83
  cache_examples=False,
84
- additional_inputs=[conversation_id_state],
85
- undo_btn=None,
86
  retry_btn="🔄 Tekrar Cevapla",
 
87
  clear_btn="🗑️ Temizle",
88
  submit_btn="Gönder",
89
  stop_btn="Durdur"
90
  )
91
 
92
- interface.retry_btn.click(fn=reset_conversation, inputs=[], outputs=[conversation_id_state])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  interface.textbox.placeholder="Mesajınızı buraya giriniz..."
94
 
95
  if __name__ == "__main__":
 
3
  import json
4
  import os
5
  import uuid
6
+ import traceback
7
 
8
  ENDPOINT = os.environ.get("ENDPOINT", None)
9
  TOKEN = os.environ.get("TOKEN", None)
 
34
 
35
  return messages
36
 
37
+ def chat_cosmosllama(message: str, history: list, conversation_id: str, parent_message_id: str) -> (str, str):
 
 
 
38
  messages = convert_to_messages(message, history)
39
 
40
  headers = {
 
42
  "Content-Type": "application/json"
43
  }
44
 
45
+ body = {"messages": messages}
46
+ body["conversation_id"] = conversation_id
47
+
48
+ if parent_message_id:
49
+ body["parent_message_id"] = parent_message_id
50
+
51
+ response = requests.post(f"https://model.cosmosytu.online{ENDPOINT}", json=body, headers=headers, stream=True)
52
+
53
+ full_response = ""
54
+ new_parent_message_id = parent_message_id
55
 
56
  if response.status_code == 200:
 
57
  for line in response.iter_lines():
58
  if line:
59
  decoded_line = line.decode('utf-8')
60
  try:
61
  json_line = json.loads(decoded_line[6:])
62
 
63
+ if "assistantMessageId" in json_line:
64
+ new_parent_message_id = json_line["assistantMessageId"]
65
+
66
  if "CosmosLlama" in json_line:
67
  full_response += json_line['CosmosLlama']
68
+ yield full_response, new_parent_message_id
69
+ except Exception:
70
+ traceback.print_exc()
 
71
  else:
72
+ yield "Bir hata oluştu.", new_parent_message_id
73
 
74
  def get_conversation_id():
75
  return str(uuid.uuid4())
76
 
77
+ def clear_conversation():
78
+ return get_conversation_id(), []
 
79
 
80
+ chatbot = gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Cosmos LLaMa')
81
 
82
  with gr.Blocks(fill_height=True, css=css) as demo:
83
+ conversation_id_state = gr.State(value=get_conversation_id())
84
+ parent_message_history = gr.State(value=[])
85
 
86
+ def handle_user_message(message, history, conversation_id_state, parent_message_history):
87
+ is_parent_message_set = False
88
+ response_generator = chat_cosmosllama(message, history, conversation_id_state, parent_message_history[-1] if parent_message_history else None)
89
+ for response, updated_parent_message_id in response_generator:
90
+ if not is_parent_message_set:
91
+ parent_message_history.append(updated_parent_message_id)
92
+ is_parent_message_set = True
93
+ yield response
94
+
95
+ def undo_message(history, parent_message_history):
96
+ if parent_message_history:
97
+ parent_message_history.pop()
98
+ history.pop()
99
+ return history, parent_message_history
100
+ return history, parent_message_history
101
+
102
+ def retry_message(history, conversation_id_state, parent_message_history):
103
+ if parent_message_history and history:
104
+ parent_message_history.pop()
105
+ history.pop()
106
+ return history, parent_message_history
107
+ return history, parent_message_history
108
+
109
  interface = gr.ChatInterface(
110
+ fn=handle_user_message,
111
  chatbot=chatbot,
112
  fill_height=True,
113
  cache_examples=False,
114
+ additional_inputs=[conversation_id_state, parent_message_history],
 
115
  retry_btn="🔄 Tekrar Cevapla",
116
+ undo_btn="↩️ Geri Al",
117
  clear_btn="🗑️ Temizle",
118
  submit_btn="Gönder",
119
  stop_btn="Durdur"
120
  )
121
 
122
+ demo.load(fn=clear_conversation, inputs=[], outputs=[conversation_id_state, parent_message_history])
123
+
124
+ interface.undo_btn.click(
125
+ fn=undo_message,
126
+ inputs=[chatbot, parent_message_history],
127
+ outputs=[chatbot, parent_message_history]
128
+ )
129
+
130
+ interface.retry_btn.click(
131
+ fn=retry_message,
132
+ inputs=[chatbot, conversation_id_state, parent_message_history],
133
+ outputs=[chatbot, parent_message_history]
134
+ )
135
+
136
+ interface.clear_btn.click(fn=clear_conversation, inputs=[], outputs=[conversation_id_state, parent_message_history])
137
  interface.textbox.placeholder="Mesajınızı buraya giriniz..."
138
 
139
  if __name__ == "__main__":