awacke1 commited on
Commit
e41bf9b
Β·
verified Β·
1 Parent(s): a917b09

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -14
app.py CHANGED
@@ -2,8 +2,6 @@ import streamlit as st
2
  import asyncio
3
  import xai_sdk
4
  import os
5
- import base64
6
- import io
7
  from datetime import datetime
8
 
9
  # Set up the XAI client
@@ -35,6 +33,12 @@ for message in st.session_state.messages:
35
 
36
  prompt = st.chat_input("What would you like to ask?")
37
 
 
 
 
 
 
 
38
  if prompt:
39
  st.session_state.messages.append({"role": "user", "content": prompt})
40
  with st.chat_message("user"):
@@ -45,14 +49,10 @@ if prompt:
45
  full_response = ""
46
 
47
  # Use asyncio to run the asynchronous XAI SDK
48
- async def generate_response():
49
- nonlocal full_response
50
- async for token in client.sampler.sample(prompt="", inputs=(prompt,), max_len=max_tokens):
51
- full_response += token.token_str
52
- message_placeholder.markdown(full_response + "β–Œ")
53
- message_placeholder.markdown(full_response)
54
-
55
- asyncio.run(generate_response())
56
 
57
  st.session_state.messages.append({"role": "assistant", "content": full_response})
58
 
@@ -60,14 +60,12 @@ if prompt:
60
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
61
  filename = f"output_{timestamp}_{prompt[:20].replace(' ', '_')}.txt"
62
 
63
- output = io.StringIO()
64
- output.write(f"Prompt: {prompt}\n\n")
65
- output.write(f"Response: {full_response}\n")
66
 
67
  # Create download button
68
  st.download_button(
69
  label="πŸ“₯ Download Output",
70
- data=output.getvalue(),
71
  file_name=filename,
72
  mime="text/plain",
73
  )
 
2
  import asyncio
3
  import xai_sdk
4
  import os
 
 
5
  from datetime import datetime
6
 
7
  # Set up the XAI client
 
33
 
34
  prompt = st.chat_input("What would you like to ask?")
35
 
36
+ async def generate_response(prompt, max_len):
37
+ full_response = ""
38
+ async for token in client.sampler.sample(prompt="", inputs=(prompt,), max_len=max_len):
39
+ full_response += token.token_str
40
+ yield full_response
41
+
42
  if prompt:
43
  st.session_state.messages.append({"role": "user", "content": prompt})
44
  with st.chat_message("user"):
 
49
  full_response = ""
50
 
51
  # Use asyncio to run the asynchronous XAI SDK
52
+ for response in asyncio.run(generate_response(prompt, max_tokens)):
53
+ message_placeholder.markdown(response + "β–Œ")
54
+ full_response = response
55
+ message_placeholder.markdown(full_response)
 
 
 
 
56
 
57
  st.session_state.messages.append({"role": "assistant", "content": full_response})
58
 
 
60
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
61
  filename = f"output_{timestamp}_{prompt[:20].replace(' ', '_')}.txt"
62
 
63
+ output_content = f"Prompt: {prompt}\n\nResponse: {full_response}\n"
 
 
64
 
65
  # Create download button
66
  st.download_button(
67
  label="πŸ“₯ Download Output",
68
+ data=output_content,
69
  file_name=filename,
70
  mime="text/plain",
71
  )