Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- src/app.py +81 -0
- src/model_info.json +6 -0
src/app.py
CHANGED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import necessary libraries
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import os
|
| 5 |
+
from openai import OpenAI
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
working_dir = os.path.dirname(os.path.abspath(__file__))
|
| 9 |
+
endpoint_data = json.load(open(f"{working_dir}/model_info.json"))
|
| 10 |
+
|
| 11 |
+
def clear_chat():
|
| 12 |
+
st.session_state.messages = []
|
| 13 |
+
|
| 14 |
+
def get_api_key():
|
| 15 |
+
# Retrieve API key from environment variable or prompt user
|
| 16 |
+
return os.getenv("OPENAI_API_KEY") or st.text_input("Enter your API Key", type="password")
|
| 17 |
+
|
| 18 |
+
st.title("Alphadata AIaaS on Intel® Gaudi®")
|
| 19 |
+
|
| 20 |
+
# Extract endpoint and model names from JSON data
|
| 21 |
+
endpoint = endpoint_data['endpoint']
|
| 22 |
+
model_names = endpoint_data['models']
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
with st.sidebar:
|
| 26 |
+
modelname = st.selectbox("Select a LLM model (Running on Intel® Gaudi®) ", model_names)
|
| 27 |
+
st.write(f"You selected: {modelname}")
|
| 28 |
+
st.button("Start New Chat", on_click=clear_chat)
|
| 29 |
+
|
| 30 |
+
# Add a text input for the API key
|
| 31 |
+
api_key = get_api_key()
|
| 32 |
+
if api_key:
|
| 33 |
+
st.session_state.api_key = api_key
|
| 34 |
+
|
| 35 |
+
# Check if the API key is provided
|
| 36 |
+
if "api_key" not in st.session_state or not st.session_state.api_key:
|
| 37 |
+
st.error("Please enter your API Key in the sidebar.")
|
| 38 |
+
else:
|
| 39 |
+
try:
|
| 40 |
+
api_key = st.session_state.api_key
|
| 41 |
+
base_url = endpoint
|
| 42 |
+
client = OpenAI(api_key=api_key, base_url=base_url)
|
| 43 |
+
|
| 44 |
+
print(f"Selected Model --> {modelname}")
|
| 45 |
+
st.write(f"**Model Info:** `{modelname}`")
|
| 46 |
+
|
| 47 |
+
if "messages" not in st.session_state:
|
| 48 |
+
st.session_state.messages = []
|
| 49 |
+
|
| 50 |
+
for message in st.session_state.messages:
|
| 51 |
+
with st.chat_message(message["role"]):
|
| 52 |
+
st.markdown(message["content"])
|
| 53 |
+
|
| 54 |
+
if prompt := st.chat_input("What is up?"):
|
| 55 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 56 |
+
with st.chat_message("user"):
|
| 57 |
+
st.markdown(prompt)
|
| 58 |
+
|
| 59 |
+
with st.chat_message("assistant"):
|
| 60 |
+
try:
|
| 61 |
+
stream = client.chat.completions.create(
|
| 62 |
+
model=modelname,
|
| 63 |
+
messages=[
|
| 64 |
+
{"role": m["role"], "content": m["content"]}
|
| 65 |
+
for m in st.session_state.messages
|
| 66 |
+
],
|
| 67 |
+
max_tokens=1024,
|
| 68 |
+
temperature=0,
|
| 69 |
+
stream=True,
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
response = st.write_stream(stream)
|
| 73 |
+
except Exception as e:
|
| 74 |
+
st.error(f"An error occurred while generating the response: {e}")
|
| 75 |
+
response = "An error occurred while generating the response."
|
| 76 |
+
|
| 77 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 78 |
+
except KeyError as e:
|
| 79 |
+
st.error(f"Key error: {e}")
|
| 80 |
+
except Exception as e:
|
| 81 |
+
st.error(f"An unexpected error occurred: {e}")
|
src/model_info.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"endpoint": "https://inference-api-demo.edgecollaborate.com/ALLaM-7B-Instruct-preview/v1/",
|
| 3 |
+
"models": [
|
| 4 |
+
"ALLaM-AI/ALLaM-7B-Instruct-preview"
|
| 5 |
+
]
|
| 6 |
+
}
|