Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
base_url = "https://internlm-chat.intern-ai.org.cn/puyu/api/v1/"
|
| 7 |
+
api_key = os.environ.get("API_KEY")
|
| 8 |
+
model="internlm2.5-latest"
|
| 9 |
+
|
| 10 |
+
client = OpenAI(api_key=api_key, base_url=base_url)
|
| 11 |
+
|
| 12 |
+
st.set_page_config(page_title="internlm-spaces", page_icon="🦜🔗")
|
| 13 |
+
st.title("internlm-spaces")
|
| 14 |
+
|
| 15 |
+
if "messages" not in st.session_state:
|
| 16 |
+
st.session_state.messages = []
|
| 17 |
+
|
| 18 |
+
for message in st.session_state.messages:
|
| 19 |
+
with st.chat_message(message['role']):
|
| 20 |
+
st.markdown(message["content"])
|
| 21 |
+
|
| 22 |
+
def response_gernerator(msg):
|
| 23 |
+
response = client.chat.completions.create(
|
| 24 |
+
model=model,
|
| 25 |
+
messages=[msg]
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
return response.choices[0].message.content
|
| 29 |
+
|
| 30 |
+
if prompt := st.chat_input("What is up?"):
|
| 31 |
+
with st.chat_message("user"):
|
| 32 |
+
st.markdown(prompt)
|
| 33 |
+
|
| 34 |
+
user_msg = {"role": "user", "content": prompt}
|
| 35 |
+
|
| 36 |
+
st.session_state.messages.append(user_msg)
|
| 37 |
+
|
| 38 |
+
response = response_gernerator(user_msg)
|
| 39 |
+
|
| 40 |
+
with st.chat_message("assistant"):
|
| 41 |
+
st.markdown(response)
|
| 42 |
+
|
| 43 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|