Spaces:
Running
Running
File size: 2,115 Bytes
c60c4b6 |
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 |
import streamlit as st
import requests
import os
# Load API Key from Hugging Face Secrets
HF_API_KEY = os.getenv("KEY")
# API URL
HF_MISTRAL_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
# Function to call Hugging Face API
def chat_with_mistral_hf(prompt):
if not HF_API_KEY:
return "Error: API key not found. Please set it in Hugging Face Secrets."
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
payload = {"inputs": prompt, "parameters": {"max_length": 200, "temperature": 0.7}}
response = requests.post(HF_MISTRAL_URL, json=payload, headers=headers)
if response.status_code == 200:
return response.json()[0]["generated_text"]
else:
return f"Error: {response.json()}"
# Streamlit UI
st.set_page_config(page_title="ChatGSKD", layout="wide")
# Sidebar: Chat title rename option
st.sidebar.header("Chat Settings")
chat_title = st.sidebar.text_input("Rename Chat:", "Temporary Chat")
# Initialize chat history if not present
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
# Display Chat Title
st.title(chat_title)
# Show chat history
for chat in st.session_state.chat_history:
with st.chat_message(chat["role"]):
st.write(chat["content"])
# Input box at the bottom
user_input = st.text_area("Ask AI:", height=100, key="query", label_visibility="collapsed")
# Generate response when user submits a prompt
if st.button("Generate Response"):
if user_input.strip():
with st.spinner("Generating response..."):
response = chat_with_mistral_hf(user_input)
# Store user query & response in chat history
st.session_state.chat_history.append({"role": "user", "content": user_input})
st.session_state.chat_history.append({"role": "assistant", "content": response})
# Refresh the page to show new messages
st.rerun()
else:
st.warning("Please enter a prompt before clicking Generate Response.") |