Spaces:
Build error
Build error
Download faq_interface.py from Imtaiz/custom-assistant: direct link, hf CLI and curl.
- Browser
- Download file 3.92 kB
-
https://huggingface.co/spaces/Imtaiz/custom-assistant/resolve/main/faq_interface.py
- Command line
-
hf download hf://spaces/Imtaiz/custom-assistant/faq_interface.py
-
curl -L -o faq_interface.py https://huggingface.co/spaces/Imtaiz/custom-assistant/resolve/main/faq_interface.py
3.92 kB
| import streamlit as st | |
| import time | |
| from openai import OpenAI | |
| import os | |
| from dotenv import load_dotenv | |
| def run_interface(config=None): | |
| '''Run the FAQ interface with dynamic configuration''' | |
| # Load environment variables | |
| load_dotenv() | |
| # Setup OpenAI client | |
| openai_api_key = os.environ.get('OPENAI_API_KEY') | |
| if not openai_api_key: | |
| st.error('OpenAI API key not found. Please add it to your environment variables.') | |
| st.stop() | |
| client = OpenAI(api_key=openai_api_key) | |
| def ensure_single_thread_id(): | |
| if "thread_id" not in st.session_state: | |
| thread = client.beta.threads.create() | |
| st.session_state.thread_id = thread.id | |
| return st.session_state.thread_id | |
| def get_avatar(role): | |
| return config.get('AVATAR_CONFIG', {}).get(role) | |
| def get_assistant_response(prompt, thread_id): | |
| message_placeholder = st.empty() | |
| i = 0 | |
| client.beta.threads.messages.create( | |
| thread_id=thread_id, | |
| role="user", | |
| content=prompt | |
| ) | |
| stream = client.beta.threads.runs.create( | |
| thread_id=thread_id, | |
| assistant_id=config['ASSISTANT_ID'], | |
| stream=True | |
| ) | |
| full_response = "" | |
| for event in stream: | |
| if not full_response: | |
| message_placeholder.markdown(f"*Thinking{config['THINKING_DOTS'][i % len(config['THINKING_DOTS'])]}*", unsafe_allow_html=True) | |
| i += 1 | |
| time.sleep(config['THINKING_INTERVAL']) | |
| if event.data.object == "thread.message.delta": | |
| for content in event.data.delta.content: | |
| if content.type == 'text': | |
| chunk = content.text.value | |
| full_response += chunk | |
| lines = full_response.split('\n') | |
| cleaned_lines = [] | |
| for line in lines: | |
| cleaned_line = ' '.join([part.strip() for part in line.split('【') if '】' not in part]) | |
| cleaned_line = cleaned_line.strip() | |
| if cleaned_line: | |
| if cleaned_line[0].isdigit() and '. ' in cleaned_line: | |
| cleaned_lines.append(cleaned_line + '\n') | |
| else: | |
| cleaned_lines.append(cleaned_line) | |
| formatted_response = '\n'.join(cleaned_lines) | |
| message_placeholder.markdown(formatted_response) | |
| time.sleep(0.01) | |
| return full_response | |
| # Load CSS and initialize interface | |
| st.markdown('''<style> | |
| /* Add your custom CSS here */ | |
| .stApp { | |
| max-width: 1200px; | |
| margin: 0 auto; | |
| } | |
| </style>''', unsafe_allow_html=True) | |
| # Initialize chat interface | |
| thread_id = ensure_single_thread_id() | |
| # Display chat messages | |
| if "messages" not in st.session_state: | |
| st.session_state.messages = [] | |
| st.session_state.messages.append({ | |
| "role": "assistant", | |
| "content": config.get('WELCOME_MESSAGE', 'Hi! How can I help you today?') | |
| }) | |
| for message in st.session_state.messages: | |
| with st.chat_message(message["role"], avatar=get_avatar(message["role"])): | |
| st.markdown(message["content"]) | |
| # Chat input | |
| if prompt := st.chat_input("Type your message here..."): | |
| st.session_state.messages.append({"role": "user", "content": prompt}) | |
| with st.chat_message("user", avatar=get_avatar("user")): | |
| st.markdown(prompt) | |
| with st.chat_message("assistant", avatar=get_avatar("assistant")): | |
| response = get_assistant_response(prompt, thread_id) | |
| st.session_state.messages.append({"role": "assistant", "content": response}) | |