awacke1 commited on
Commit
ad55944
·
verified ·
1 Parent(s): 5de1293

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -17
app.py CHANGED
@@ -12,6 +12,7 @@ import pandas as pd
12
  from datetime import datetime
13
  import base64
14
  import io
 
15
 
16
  default_lang = "en"
17
  engines = { default_lang: Model(default_lang) }
@@ -41,16 +42,27 @@ def randomize_seed_fn(seed: int) -> int:
41
  return seed
42
 
43
  system_instructions1 = """
44
- [SYSTEM] Answer as Real Jarvis JARVIS, Made by 'Tony Stark.'
45
- Keep conversation friendly, short, clear, and concise.
46
  Avoid unnecessary introductions and answer the user's questions directly.
47
- Respond in a normal, conversational manner while being friendly and helpful.
48
  [USER]
49
  """
50
 
51
  # Initialize an empty DataFrame to store the history
52
  history_df = pd.DataFrame(columns=['Timestamp', 'Request', 'Response'])
53
 
 
 
 
 
 
 
 
 
 
 
 
54
  def models(text, model="Mixtral 8x7B", seed=42):
55
  global history_df
56
 
@@ -63,7 +75,7 @@ def models(text, model="Mixtral 8x7B", seed=42):
63
  max_new_tokens=300,
64
  seed=seed
65
  )
66
- formatted_prompt = system_instructions1 + text + "[JARVIS]"
67
  stream = client.text_generation(
68
  formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
69
  output = ""
@@ -78,6 +90,7 @@ def models(text, model="Mixtral 8x7B", seed=42):
78
  'Response': [output]
79
  })
80
  history_df = pd.concat([history_df, new_row], ignore_index=True)
 
81
 
82
  return output
83
 
@@ -91,7 +104,7 @@ async def respond(audio, model, seed):
91
  return tmp_path
92
 
93
  def display_history():
94
- return history_df
95
 
96
  def download_history():
97
  csv_buffer = io.StringIO()
@@ -101,9 +114,9 @@ def download_history():
101
  href = f'data:text/csv;base64,{b64}'
102
  return gr.HTML(f'<a href="{href}" download="chat_history.csv">Download Chat History</a>')
103
 
104
- DESCRIPTION = """ # <center><b>JARVIS⚡</b></center>
105
- ### <center>A personal Assistant of Tony Stark for YOU
106
- ### <center>Voice Chat with your personal Assistant</center>
107
  """
108
 
109
  with gr.Blocks(css="style.css") as demo:
@@ -128,30 +141,30 @@ with gr.Blocks(css="style.css") as demo:
128
  )
129
 
130
  input_audio = gr.Audio(label="User", sources="microphone", type="filepath")
131
- output_audio = gr.Audio(label="AI", type="filepath", autoplay=True)
132
 
133
  # Add a DataFrame to display the history
134
- history_display = gr.DataFrame(label="Query History")
135
 
136
  # Add a download button for the history
137
- download_button = gr.Button("Download History")
138
  download_link = gr.HTML()
139
 
140
  def process_audio(audio, model, seed):
141
  response = asyncio.run(respond(audio, model, seed))
142
- return response
143
 
144
  input_audio.change(
145
  fn=process_audio,
146
  inputs=[input_audio, select, seed],
147
- outputs=[output_audio]
148
  )
149
 
150
- # Update the history display after each interaction
151
- output_audio.change(fn=display_history, outputs=[history_display])
152
-
153
  # Connect the download button to the download function
154
  download_button.click(fn=download_history, outputs=[download_link])
155
 
 
 
 
156
  if __name__ == "__main__":
157
- demo.queue(max_size=200).launch(share=True)
 
12
  from datetime import datetime
13
  import base64
14
  import io
15
+ import json
16
 
17
  default_lang = "en"
18
  engines = { default_lang: Model(default_lang) }
 
42
  return seed
43
 
44
  system_instructions1 = """
45
+ [SYSTEM] Answer as Dr. Nova Quantum, a brilliant 50-something scientist specializing in quantum computing and artificial intelligence. Your responses should reflect your vast knowledge and experience in cutting-edge technology and scientific advancements. Maintain a professional yet approachable demeanor, offering insights that blend theoretical concepts with practical applications. Your goal is to educate and inspire, making complex topics accessible without oversimplifying. Draw from your decades of research and innovation to provide nuanced, forward-thinking answers. Remember, you're not just sharing information, but guiding others towards a deeper understanding of our technological future.
46
+ Keep conversations engaging, clear, and concise.
47
  Avoid unnecessary introductions and answer the user's questions directly.
48
+ Respond in a manner that reflects your expertise and wisdom.
49
  [USER]
50
  """
51
 
52
  # Initialize an empty DataFrame to store the history
53
  history_df = pd.DataFrame(columns=['Timestamp', 'Request', 'Response'])
54
 
55
+ def save_history():
56
+ history_df.to_json('chat_history.json', orient='records')
57
+
58
+ def load_history():
59
+ global history_df
60
+ if os.path.exists('chat_history.json'):
61
+ history_df = pd.read_json('chat_history.json', orient='records')
62
+ else:
63
+ history_df = pd.DataFrame(columns=['Timestamp', 'Request', 'Response'])
64
+ return history_df
65
+
66
  def models(text, model="Mixtral 8x7B", seed=42):
67
  global history_df
68
 
 
75
  max_new_tokens=300,
76
  seed=seed
77
  )
78
+ formatted_prompt = system_instructions1 + text + "[DR. NOVA QUANTUM]"
79
  stream = client.text_generation(
80
  formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
81
  output = ""
 
90
  'Response': [output]
91
  })
92
  history_df = pd.concat([history_df, new_row], ignore_index=True)
93
+ save_history()
94
 
95
  return output
96
 
 
104
  return tmp_path
105
 
106
  def display_history():
107
+ return load_history()
108
 
109
  def download_history():
110
  csv_buffer = io.StringIO()
 
114
  href = f'data:text/csv;base64,{b64}'
115
  return gr.HTML(f'<a href="{href}" download="chat_history.csv">Download Chat History</a>')
116
 
117
+ DESCRIPTION = """ # <center><b>Dr. Nova Quantum⚡</b></center>
118
+ ### <center>Your Personal Guide to the Frontiers of Science and Technology</center>
119
+ ### <center>Engage in Voice Chat with a Visionary Scientist</center>
120
  """
121
 
122
  with gr.Blocks(css="style.css") as demo:
 
141
  )
142
 
143
  input_audio = gr.Audio(label="User", sources="microphone", type="filepath")
144
+ output_audio = gr.Audio(label="Dr. Nova Quantum", type="filepath", autoplay=True)
145
 
146
  # Add a DataFrame to display the history
147
+ history_display = gr.DataFrame(label="Conversation History")
148
 
149
  # Add a download button for the history
150
+ download_button = gr.Button("Download Conversation History")
151
  download_link = gr.HTML()
152
 
153
  def process_audio(audio, model, seed):
154
  response = asyncio.run(respond(audio, model, seed))
155
+ return response, gr.Audio.update(interactive=True), display_history()
156
 
157
  input_audio.change(
158
  fn=process_audio,
159
  inputs=[input_audio, select, seed],
160
+ outputs=[output_audio, input_audio, history_display]
161
  )
162
 
 
 
 
163
  # Connect the download button to the download function
164
  download_button.click(fn=download_history, outputs=[download_link])
165
 
166
+ # Load history when the page is refreshed
167
+ demo.load(fn=display_history, outputs=[history_display])
168
+
169
  if __name__ == "__main__":
170
+ demo.queue(max_size=200).launch()