macadeliccc commited on
Commit
5c1dadc
·
1 Parent(s): 3d71752
Files changed (1) hide show
  1. app.py +30 -11
app.py CHANGED
@@ -29,30 +29,49 @@ def start_ochat_server():
29
  start_ochat_server()
30
 
31
  # Function to send a message to the ochat server and get a response
32
- def chat_with_ochat(message):
33
  url = "http://0.0.0.0:18888/v1/chat/completions"
34
  headers = {"Content-Type": "application/json"}
 
 
 
 
 
 
 
 
35
  data = {
36
  "model": "openchat_3.5",
37
- "messages": [{"role": "user", "content": message}]
38
  }
39
 
40
  try:
41
  response = requests.post(url, json=data, headers=headers)
42
  if response.status_code == 200:
 
43
  return response.json()['choices'][0]['message']['content']
44
  else:
45
  return f"Error: Server responded with status code {response.status_code}"
46
  except requests.RequestException as e:
47
  return f"Error: {e}"
48
 
49
- # Create a Gradio Interface
50
- iface = gr.Interface(
51
- fn=chat_with_ochat,
52
- inputs="text",
53
- outputs="text",
54
- title="ochat Chat Interface",
55
- description="Type your message and get a response from the ochat server."
56
- )
57
 
58
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  start_ochat_server()
30
 
31
  # Function to send a message to the ochat server and get a response
32
+ def chat_with_ochat(message, history):
33
  url = "http://0.0.0.0:18888/v1/chat/completions"
34
  headers = {"Content-Type": "application/json"}
35
+
36
+ # Convert history to the expected format
37
+ messages = [{"role": "user" if i % 2 == 0 else "system", "content": msg}
38
+ for i, (user_msg, bot_msg) in enumerate(history) for msg in [user_msg, bot_msg] if msg]
39
+
40
+ # Append the latest user message
41
+ messages.append({"role": "user", "content": message})
42
+
43
  data = {
44
  "model": "openchat_3.5",
45
+ "messages": messages
46
  }
47
 
48
  try:
49
  response = requests.post(url, json=data, headers=headers)
50
  if response.status_code == 200:
51
+ # Extract the latest bot response
52
  return response.json()['choices'][0]['message']['content']
53
  else:
54
  return f"Error: Server responded with status code {response.status_code}"
55
  except requests.RequestException as e:
56
  return f"Error: {e}"
57
 
 
 
 
 
 
 
 
 
58
 
59
+ with gr.Blocks() as demo:
60
+ chatbot = gr.Chatbot()
61
+ msg = gr.Textbox()
62
+ clear = gr.Button("Clear")
63
+
64
+ def user(user_message, history):
65
+ return "", history + [[user_message, None]]
66
+
67
+ def bot(history):
68
+ response = chat_with_ochat(history[-1][0], history)
69
+ history[-1][1] = response
70
+ return history
71
+
72
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
73
+ bot, chatbot, chatbot
74
+ )
75
+ clear.click(lambda: None, None, chatbot, queue=False)
76
+
77
+ demo.launch()