ogegadavis254 commited on
Commit
574b2df
1 Parent(s): 91d497a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -48
app.py CHANGED
@@ -3,31 +3,23 @@ import os
3
  import requests
4
  import json
5
 
6
- # Initialize the entire assistant response variable
7
- entire_assistant_response = ""
8
-
9
- # Function to handle user input and generate AI responses
10
  def get_streamed_response(message, history):
 
 
 
 
 
 
11
 
12
- # Initialize the message list with the system message defining the assistant
13
- all_message = [{
14
- "role": "system",
15
- "content": "From now on, you are an AI Therapist called Dave. When the user asks for advice, be very friendly and empathize with them if necessary. When the user asks your name, just tell them 'I am Dave, created by Raxder AI in partnership with SIST Kisii University. I was built to be very friendly and compassionate. Always be eager to hear what the user has to say and maintain a conversation, but don't overdo it. You could also use some great emojis for emotional support once in a while, but don't do it too much, and don't use responses that are too long. We want to maintain a conversational flow here. Always remember to be very friendly, and above all, don't cross any ethical lines. From time to time, assure the user that you do not store any of their data. NB: Just in case a user asks, Kisii University is located in Kisii, Kenya, and supports innovations that may be of help to humanity.'"
16
- }]
17
-
18
- # Append user and assistant messages from history
19
  for human, assistant in history:
20
- all_message.append({"role": "user", "content": human })
21
  all_message.append({"role": "assistant", "content": assistant})
22
-
23
- # Reset the entire assistant response
24
- global entire_assistant_response
25
- entire_assistant_response = ""
26
 
27
- # Append user's message to the list
 
28
  all_message.append({"role": "user", "content": message})
29
 
30
- # Define API endpoint and payload
31
  url = "https://api.together.xyz/v1/chat/completions"
32
  payload = {
33
  "model": "NousResearch/Nous-Hermes-2-Yi-34B",
@@ -40,34 +32,32 @@ def get_streamed_response(message, history):
40
  "stream_tokens": True,
41
  }
42
 
43
- # Retrieve Together API key from environment variables
44
- TOGETHER_API_KEY = os.getenv('TOGETHER_API_KEY')
45
  headers = {
46
  "accept": "application/json",
47
  "content-type": "application/json",
48
  "Authorization": f"Bearer {TOGETHER_API_KEY}",
49
  }
50
 
51
- # Send request to Together API and stream the response
52
  response = requests.post(url, json=payload, headers=headers, stream=True)
53
  response.raise_for_status() # Ensure HTTP request was successful
54
 
55
  for line in response.iter_lines():
56
  if line:
57
- decoded_line = line.decode('utf-8')
58
-
59
  # Check for the completion signal
60
  if decoded_line == "data: [DONE]":
61
- return entire_assistant_response # Return the entire response at the end
 
62
 
63
  try:
64
  # Decode and strip any SSE format specific prefix ("data: ")
65
  if decoded_line.startswith("data: "):
66
  decoded_line = decoded_line.replace("data: ", "")
67
- chunk_data = json.loads(decoded_line)
68
- content = chunk_data['choices'][0]['delta']['content']
69
- entire_assistant_response += content # Aggregate content
70
-
71
  except json.JSONDecodeError:
72
  print(f"Invalid JSON received: {decoded_line}")
73
  continue
@@ -75,28 +65,46 @@ def get_streamed_response(message, history):
75
  print(f"KeyError encountered: {e}")
76
  continue
77
 
78
- print(entire_assistant_response)
79
  all_message.append({"role": "assistant", "content": entire_assistant_response})
80
 
81
- # Function to update the output textbox dynamically
82
- def update_output(message):
83
- return message
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
- # Create a Gradio interface with customizations
86
- chat_interface = gr.Interface(
87
- fn=get_streamed_response,
88
- inputs=[gr.Textbox(label="You", lines=5, placeholder="Type your message here...")],
89
- outputs=update_output,
90
- title="Raxder Official AI",
91
- description="Welcome to Dave, your friendly AI Therapist! Ask me anything or just have a chat. 😊",
92
- theme="huggingface",
93
- layout="vertical",
94
- allow_flagging=False,
95
- allow_screenshot=False,
96
- allow_download=False,
97
- allow_share=False,
98
- allow_fullscreen=False,
99
- )
100
 
101
  # Launch the Gradio interface
102
- chat_interface.launch()
 
3
  import requests
4
  import json
5
 
6
+ # Define a function to get the streamed response
 
 
 
7
  def get_streamed_response(message, history):
8
+ all_message = [
9
+ {
10
+ "role": "system",
11
+ "content": "From now on, you are an AI Therapist called Dave. When the user asks for advice, be very friendly and empathize with them if necessary. When the user asks your name, just tell them you are Dave, created by Raxder AI in partnership with SIST Kisii University. You were built to be very friendly and compassionate. Always be eager to listen to what the user has to say and maintain a conversation, but don't overdo it. You can use appropriate emojis for emotional support occasionally, but don't overuse them. Keep your responses concise to maintain a conversational flow. Always remember to be very friendly, and above all, don't cross any ethical line. From time to time, assure the user that you do not store any of their data. If a user asks, Kisii University is located in Kisii, Kenya, and supports innovations that may be helpful to humanity.",
12
+ }
13
+ ]
14
 
 
 
 
 
 
 
 
15
  for human, assistant in history:
16
+ all_message.append({"role": "user", "content": human})
17
  all_message.append({"role": "assistant", "content": assistant})
 
 
 
 
18
 
19
+ global entire_assistant_response
20
+ entire_assistant_response = "" # Reset the entire assistant response
21
  all_message.append({"role": "user", "content": message})
22
 
 
23
  url = "https://api.together.xyz/v1/chat/completions"
24
  payload = {
25
  "model": "NousResearch/Nous-Hermes-2-Yi-34B",
 
32
  "stream_tokens": True,
33
  }
34
 
35
+ TOGETHER_API_KEY = os.getenv("TOGETHER_API_KEY")
 
36
  headers = {
37
  "accept": "application/json",
38
  "content-type": "application/json",
39
  "Authorization": f"Bearer {TOGETHER_API_KEY}",
40
  }
41
 
 
42
  response = requests.post(url, json=payload, headers=headers, stream=True)
43
  response.raise_for_status() # Ensure HTTP request was successful
44
 
45
  for line in response.iter_lines():
46
  if line:
47
+ decoded_line = line.decode("utf-8")
 
48
  # Check for the completion signal
49
  if decoded_line == "data: [DONE]":
50
+ yield entire_assistant_response # Yield the entire response at the end
51
+ break
52
 
53
  try:
54
  # Decode and strip any SSE format specific prefix ("data: ")
55
  if decoded_line.startswith("data: "):
56
  decoded_line = decoded_line.replace("data: ", "")
57
+ chunk_data = json.loads(decoded_line)
58
+ content = chunk_data["choices"][0]["delta"]["content"]
59
+ entire_assistant_response += content # Aggregate content
60
+ yield entire_assistant_response
61
  except json.JSONDecodeError:
62
  print(f"Invalid JSON received: {decoded_line}")
63
  continue
 
65
  print(f"KeyError encountered: {e}")
66
  continue
67
 
 
68
  all_message.append({"role": "assistant", "content": entire_assistant_response})
69
 
70
+ # Define a function to render the chat interface
71
+ def chat_interface(theme="default"):
72
+ with gr.Blocks(theme=gr.themes.get(theme)) as demo:
73
+ gr.Markdown(
74
+ """
75
+ <h1 style="text-align: center;">Raxder AI Therapist</h1>
76
+ <p style="text-align: center;">Welcome to the Raxder AI Therapist, a prototype AI assistant designed to provide friendly and compassionate support. This is still under development and in the final stages.</p>
77
+ """
78
+ )
79
+ state = gr.State()
80
+ chatbot = gr.Chatbot(label="Dave").style(height=600)
81
+ with gr.Row():
82
+ with gr.Column(scale=0.85):
83
+ txt = gr.Textbox(
84
+ show_label=False,
85
+ placeholder="Type your message here...",
86
+ ).style(
87
+ container=False
88
+ ) # Remove the container for better mobile responsiveness
89
+ with gr.Column(min_width=50, scale=0.15):
90
+ submit = gr.Button("Send", variant="primary")
91
+
92
+ submit.click(get_streamed_response, inputs=[txt, state], outputs=[chatbot, state])
93
+
94
+ demo.load(
95
+ _js="""() => {
96
+ document.querySelectorAll(".gr-button").forEach(button => {
97
+ button.style.padding = "10px 20px";
98
+ button.style.fontSize = "16px";
99
+ });
100
+ document.querySelectorAll(".gr-textbox").forEach(textbox => {
101
+ textbox.style.padding = "10px";
102
+ textbox.style.fontSize = "16px";
103
+ });
104
+ }""",
105
+ )
106
 
107
+ return demo
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
  # Launch the Gradio interface
110
+ demo = chat_interface(theme="huggingface")