File size: 4,566 Bytes
0a877e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
269959c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a877e5
 
 
 
 
 
269959c
0a877e5
 
 
 
 
269959c
0a877e5
 
 
 
 
 
269959c
0a877e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#    _____ _    _ _____    _____ _           _   _           _   
#   / ____| |  | |_   _|  / ____| |         | | | |         | |  
#  | |  __| |  | | | |   | |    | |__   __ _| |_| |__   ___ | |_ 
#  | | |_ | |  | | | |   | |    | '_ \ / _` | __| '_ \ / _ \| __|
#  | |__| | |__| |_| |_  | |____| | | | (_| | |_| |_) | (_) | |_ 
#   \_____|\____/|_____|  \_____|_| |_|\__,_|\__|_.__/ \___/ \__|
#                                                         
#    __________  ___  ___________  _____   ___ 
#   /  _/_  __/ / _ )/ __/_  __| \/ / _ | / _ \
#  _/ /  / /   / _  / _/  / /   \  / __ |/ , _/
# /___/ /_/   /____/___/ /_/    /_/_/ |_/_/|_| 
#
# SOFT PILLOW VERZIÓ
# HF - Streamlit verzió

import streamlit as st
import time
from openai import OpenAI
import os

# Get secrets from environment variables
api_key = os.environ.get('OPENAI_API_KEY')
assistant_id = os.environ.get('ASSISTANT_ID')

# Initialize OpenAI client
client = OpenAI(api_key=api_key)

# Verify credentials are loaded
if not api_key or not assistant_id:
    st.error("Please set up OPENAI_API_KEY and ASSISTANT_ID in your Hugging Face Space secrets.")
    st.stop()

# Custom avatars
assistant_avatar = "https://huggingface.co/spaces/InfoBetyar/RAG-Chat/resolve/main/puhaparnaicon.webp"
user_avatar = "https://huggingface.co/spaces/InfoBetyar/RAG-Chat/resolve/main/usericon.webp" 

# Center-align the image and headers
col1, col2 = st.columns([1, 2])  # The ratio 1:2 gives the second column more space

# Left column: Image
with col1:
    st.image("https://huggingface.co/spaces/InfoBetyar/RAG-Chat/resolve/main/puhaparna.webp", width=200)

# Right column: Titles
with col2:
    st.markdown("""
        <div style="margin-top: -30px;">  <!-- Added padding to vertically align with image -->
            <h1 style="margin: 0;">Soft Pillow Apartment - Chatbot</h1>
            <h3 style="margin: 0;">IT Betyár - Minta alkalmazás diákoknak</h3>
        </div>
        """, unsafe_allow_html=True)

# Initialize messages in session state if not present
if "messages" not in st.session_state:
    st.session_state.messages = []

# Display existing messages
for message in st.session_state.messages:
    with st.chat_message(message["role"], avatar=assistant_avatar if message["role"] == "assistant" else user_avatar):
        st.write(message["content"])

# Chat input
if prompt := st.chat_input("Type your message..."):
    # Display user message
    with st.chat_message("user", avatar=user_avatar):
        st.write(prompt)
    
    # Add user message to state
    st.session_state.messages.append({"role": "user", "content": prompt})

    # Get assistant response
    with st.chat_message("assistant", avatar=assistant_avatar):
        with st.spinner("Thinking..."):
            try:
                # Create thread and send message
                thread = client.beta.threads.create()
                client.beta.threads.messages.create(
                    thread_id=thread.id,
                    role="user",
                    content=prompt
                )

                # Run assistant
                run = client.beta.threads.runs.create(
                    thread_id=thread.id,
                    assistant_id=assistant_id
                )

                # Wait for completion
                while run.status != "completed":
                    time.sleep(0.5)
                    run = client.beta.threads.runs.retrieve(
                        thread_id=thread.id,
                        run_id=run.id
                    )

                # Get and display response
                messages = client.beta.threads.messages.list(thread_id=thread.id)
                response = messages.data[0].content[0].text.value
                cleaned_response = response.split("【")[0]
                st.write(cleaned_response)

                # Add assistant response to state
                st.session_state.messages.append(
                    {"role": "assistant", "content": cleaned_response}
                )

                # Clean up thread
                client.beta.threads.delete(thread.id)
                
            except Exception as e:
                st.error(f"An error occurred: {str(e)}")

# Teszt üzenetek:
#-------------------------------------
# What's the check in time?
# What are the check-in and check-out times?
# Are there any local restaurants you'd suggest?
# What's the Wi-Fi password for the property?
# What's the pin for the lockbox?
# What is the check-out procedure?
# What is the manager's name?     -->   NEM kell tudja!