Spaces:
Runtime error
Runtime error
File size: 2,494 Bytes
f12a563 a917b09 f12a563 a917b09 e41bf9b a917b09 e41bf9b a917b09 e41bf9b a917b09 e41bf9b a917b09 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
import streamlit as st
import asyncio
import xai_sdk
import os
from datetime import datetime
# Set up the XAI client
api_key = st.sidebar.text_input("Enter your XAI API key", type="password")
os.environ["XAI_API_KEY"] = api_key
# Initialize the XAI client
@st.cache_resource
def get_xai_client():
return xai_sdk.Client()
client = get_xai_client()
# Streamlit app
st.title("π€ XAI Streamlit Client")
# Sidebar
st.sidebar.header("Settings")
max_tokens = st.sidebar.slider("Max Tokens", 1, 100, 50)
temperature = st.sidebar.slider("Temperature", 0.0, 1.0, 0.7)
# Chat interface
if "messages" not in st.session_state:
st.session_state.messages = []
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
prompt = st.chat_input("What would you like to ask?")
async def generate_response(prompt, max_len):
full_response = ""
async for token in client.sampler.sample(prompt="", inputs=(prompt,), max_len=max_len):
full_response += token.token_str
yield full_response
if prompt:
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
with st.chat_message("assistant"):
message_placeholder = st.empty()
full_response = ""
# Use asyncio to run the asynchronous XAI SDK
for response in asyncio.run(generate_response(prompt, max_tokens)):
message_placeholder.markdown(response + "β")
full_response = response
message_placeholder.markdown(full_response)
st.session_state.messages.append({"role": "assistant", "content": full_response})
# Generate output file
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"output_{timestamp}_{prompt[:20].replace(' ', '_')}.txt"
output_content = f"Prompt: {prompt}\n\nResponse: {full_response}\n"
# Create download button
st.download_button(
label="π₯ Download Output",
data=output_content,
file_name=filename,
mime="text/plain",
)
# Add some helpful information
st.sidebar.markdown("---")
st.sidebar.subheader("π How to use")
st.sidebar.markdown("""
1. Enter your XAI API key in the sidebar
2. Adjust the settings as needed
3. Type your prompt in the chat input
4. Download the output if desired
""")
# Footer
st.sidebar.markdown("---")
st.sidebar.markdown("Made with β€οΈ using Streamlit and XAI SDK") |