Spaces:
Running
Running
Update chatgskd.py
Browse files- chatgskd.py +61 -61
chatgskd.py
CHANGED
@@ -1,61 +1,61 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import requests
|
3 |
-
import os
|
4 |
-
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
# API
|
9 |
-
|
10 |
-
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
# Sidebar: Chat title rename option
|
30 |
-
st.sidebar.header("Chat Settings")
|
31 |
-
chat_title = st.sidebar.text_input("Rename Chat:", "Temporary Chat")
|
32 |
-
|
33 |
-
# Initialize chat history if not present
|
34 |
-
if "chat_history" not in st.session_state:
|
35 |
-
st.session_state.chat_history = []
|
36 |
-
|
37 |
-
# Display Chat Title
|
38 |
-
st.title(chat_title)
|
39 |
-
|
40 |
-
# Show chat history
|
41 |
-
for chat in st.session_state.chat_history:
|
42 |
-
with st.chat_message(chat["role"]):
|
43 |
-
st.write(chat["content"])
|
44 |
-
|
45 |
-
# Input box at the bottom
|
46 |
-
user_input = st.text_area("Ask AI:", height=100, key="query", label_visibility="collapsed")
|
47 |
-
|
48 |
-
# Generate response when user submits a prompt
|
49 |
-
if st.button("Generate Response"):
|
50 |
-
if user_input.strip():
|
51 |
-
with st.spinner("Generating response..."):
|
52 |
-
response = chat_with_mistral_hf(user_input)
|
53 |
-
|
54 |
-
# Store user query & response in chat history
|
55 |
-
st.session_state.chat_history.append({"role": "user", "content": user_input})
|
56 |
-
st.session_state.chat_history.append({"role": "assistant", "content": response})
|
57 |
-
|
58 |
-
# Refresh the page to show new messages
|
59 |
-
st.rerun()
|
60 |
-
else:
|
61 |
-
st.warning("Please enter a prompt before clicking Generate Response.")
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Set page config **as the first Streamlit command**
|
6 |
+
st.set_page_config(page_title="ChatGSKD", layout="wide")
|
7 |
+
|
8 |
+
# Load API Key from Hugging Face Secrets
|
9 |
+
HF_API_KEY = os.getenv("KEY")
|
10 |
+
|
11 |
+
# API URL
|
12 |
+
HF_MISTRAL_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
|
13 |
+
|
14 |
+
# Function to call Hugging Face API
|
15 |
+
def chat_with_mistral_hf(prompt):
|
16 |
+
if not HF_API_KEY:
|
17 |
+
return "Error: API key not found. Please set it in Hugging Face Secrets."
|
18 |
+
|
19 |
+
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
|
20 |
+
payload = {"inputs": prompt, "parameters": {"max_length": 200, "temperature": 0.7}}
|
21 |
+
|
22 |
+
response = requests.post(HF_MISTRAL_URL, json=payload, headers=headers)
|
23 |
+
|
24 |
+
if response.status_code == 200:
|
25 |
+
return response.json()[0]["generated_text"]
|
26 |
+
else:
|
27 |
+
return f"Error: {response.json()}"
|
28 |
+
|
29 |
+
# Sidebar: Chat title rename option
|
30 |
+
st.sidebar.header("Chat Settings")
|
31 |
+
chat_title = st.sidebar.text_input("Rename Chat:", "Temporary Chat")
|
32 |
+
|
33 |
+
# Initialize chat history if not present
|
34 |
+
if "chat_history" not in st.session_state:
|
35 |
+
st.session_state.chat_history = []
|
36 |
+
|
37 |
+
# Display Chat Title
|
38 |
+
st.title(chat_title)
|
39 |
+
|
40 |
+
# Show chat history
|
41 |
+
for chat in st.session_state.chat_history:
|
42 |
+
with st.chat_message(chat["role"]):
|
43 |
+
st.write(chat["content"])
|
44 |
+
|
45 |
+
# Input box at the bottom
|
46 |
+
user_input = st.text_area("Ask AI:", height=100, key="query", label_visibility="collapsed")
|
47 |
+
|
48 |
+
# Generate response when user submits a prompt
|
49 |
+
if st.button("Generate Response"):
|
50 |
+
if user_input.strip():
|
51 |
+
with st.spinner("Generating response..."):
|
52 |
+
response = chat_with_mistral_hf(user_input)
|
53 |
+
|
54 |
+
# Store user query & response in chat history
|
55 |
+
st.session_state.chat_history.append({"role": "user", "content": user_input})
|
56 |
+
st.session_state.chat_history.append({"role": "assistant", "content": response})
|
57 |
+
|
58 |
+
# Refresh the page to show new messages
|
59 |
+
st.rerun()
|
60 |
+
else:
|
61 |
+
st.warning("Please enter a prompt before clicking Generate Response.")
|