Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,87 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
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
|
10 |
+
api_key = st.sidebar.text_input("Enter your XAI API key", type="password")
|
11 |
+
os.environ["XAI_API_KEY"] = api_key
|
12 |
+
|
13 |
+
# Initialize the XAI client
|
14 |
+
@st.cache_resource
|
15 |
+
def get_xai_client():
|
16 |
+
return xai_sdk.Client()
|
17 |
+
|
18 |
+
client = get_xai_client()
|
19 |
+
|
20 |
+
# Streamlit app
|
21 |
+
st.title("π€ XAI Streamlit Client")
|
22 |
+
|
23 |
+
# Sidebar
|
24 |
+
st.sidebar.header("Settings")
|
25 |
+
max_tokens = st.sidebar.slider("Max Tokens", 1, 100, 50)
|
26 |
+
temperature = st.sidebar.slider("Temperature", 0.0, 1.0, 0.7)
|
27 |
+
|
28 |
+
# Chat interface
|
29 |
+
if "messages" not in st.session_state:
|
30 |
+
st.session_state.messages = []
|
31 |
+
|
32 |
+
for message in st.session_state.messages:
|
33 |
+
with st.chat_message(message["role"]):
|
34 |
+
st.markdown(message["content"])
|
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"):
|
41 |
+
st.markdown(prompt)
|
42 |
+
|
43 |
+
with st.chat_message("assistant"):
|
44 |
+
message_placeholder = st.empty()
|
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 |
+
|
59 |
+
# Generate output file
|
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 |
+
)
|
74 |
+
|
75 |
+
# Add some helpful information
|
76 |
+
st.sidebar.markdown("---")
|
77 |
+
st.sidebar.subheader("π How to use")
|
78 |
+
st.sidebar.markdown("""
|
79 |
+
1. Enter your XAI API key in the sidebar
|
80 |
+
2. Adjust the settings as needed
|
81 |
+
3. Type your prompt in the chat input
|
82 |
+
4. Download the output if desired
|
83 |
+
""")
|
84 |
+
|
85 |
+
# Footer
|
86 |
+
st.sidebar.markdown("---")
|
87 |
+
st.sidebar.markdown("Made with β€οΈ using Streamlit and XAI SDK")
|