InfoBetyar commited on
Commit
0a877e5
·
verified ·
1 Parent(s): 7b60bff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -107
app.py CHANGED
@@ -1,107 +1,117 @@
1
- # _____ _ _ _____ _____ _ _ _ _
2
- # / ____| | | |_ _| / ____| | | | | | | |
3
- # | | __| | | | | | | | | |__ __ _| |_| |__ ___ | |_
4
- # | | |_ | | | | | | | | | '_ \ / _` | __| '_ \ / _ \| __|
5
- # | |__| | |__| |_| |_ | |____| | | | (_| | |_| |_) | (_) | |_
6
- # \_____|\____/|_____| \_____|_| |_|\__,_|\__|_.__/ \___/ \__|
7
- #
8
- # __________ ___ ___________ _____ ___
9
- # / _/_ __/ / _ )/ __/_ __| \/ / _ | / _ \
10
- # _/ / / / / _ / _/ / / \ / __ |/ , _/
11
- # /___/ /_/ /____/___/ /_/ /_/_/ |_/_/|_|
12
- #
13
- # SOFT PILLOW VERZIÓ
14
- # HF - Streamlit verzió
15
-
16
- import streamlit as st
17
- import time
18
- from openai import OpenAI
19
- import os
20
-
21
- # Get secrets from environment variables
22
- api_key = os.environ.get('OPENAI_API_KEY')
23
- assistant_id = os.environ.get('ASSISTANT_ID')
24
-
25
- # Initialize OpenAI client
26
- client = OpenAI(api_key=api_key)
27
-
28
- # Verify credentials are loaded
29
- if not api_key or not assistant_id:
30
- st.error("Please set up OPENAI_API_KEY and ASSISTANT_ID in your Hugging Face Space secrets.")
31
- st.stop()
32
-
33
- # Streamlit App Title
34
- st.title("IT Betyár - ChatBot")
35
-
36
- # Initialize messages in session state if not present
37
- if "messages" not in st.session_state:
38
- st.session_state.messages = []
39
-
40
- # Display existing messages
41
- for message in st.session_state.messages:
42
- with st.chat_message(message["role"]):
43
- st.write(message["content"])
44
-
45
- # Chat input
46
- if prompt := st.chat_input("Type your message..."):
47
- # Display user message
48
- with st.chat_message("user"):
49
- st.write(prompt)
50
-
51
- # Add user message to state
52
- st.session_state.messages.append({"role": "user", "content": prompt})
53
-
54
- # Get assistant response
55
- with st.chat_message("assistant"):
56
- with st.spinner("Thinking..."):
57
- try:
58
- # Create thread and send message
59
- thread = client.beta.threads.create()
60
- client.beta.threads.messages.create(
61
- thread_id=thread.id,
62
- role="user",
63
- content=prompt
64
- )
65
-
66
- # Run assistant
67
- run = client.beta.threads.runs.create(
68
- thread_id=thread.id,
69
- assistant_id=assistant_id
70
- )
71
-
72
- # Wait for completion
73
- while run.status != "completed":
74
- time.sleep(0.5)
75
- run = client.beta.threads.runs.retrieve(
76
- thread_id=thread.id,
77
- run_id=run.id
78
- )
79
-
80
- # Get and display response
81
- messages = client.beta.threads.messages.list(thread_id=thread.id)
82
- response = messages.data[0].content[0].text.value
83
- cleaned_response = response.split("")[0]
84
- st.write(cleaned_response)
85
-
86
- # Add assistant response to state
87
- st.session_state.messages.append(
88
- {"role": "assistant", "content": cleaned_response}
89
- )
90
-
91
- # Clean up thread
92
- client.beta.threads.delete(thread.id)
93
-
94
- except Exception as e:
95
- st.error(f"An error occurred: {str(e)}")
96
-
97
-
98
- # Teszt üzenetek:
99
- #-------------------------------------
100
- # What's the check in time?
101
- # What are the check-in and check-out times?
102
- # Are there any local restaurants you'd suggest?
103
- # What's the Wi-Fi password for the property?
104
- # What's the pin for the lockbox?
105
- # What is the check-out procedure?
106
- # What is the manager's name? --> NEM kell tudja!
107
-
 
 
 
 
 
 
 
 
 
 
 
1
+ # _____ _ _ _____ _____ _ _ _ _
2
+ # / ____| | | |_ _| / ____| | | | | | | |
3
+ # | | __| | | | | | | | | |__ __ _| |_| |__ ___ | |_
4
+ # | | |_ | | | | | | | | | '_ \ / _` | __| '_ \ / _ \| __|
5
+ # | |__| | |__| |_| |_ | |____| | | | (_| | |_| |_) | (_) | |_
6
+ # \_____|\____/|_____| \_____|_| |_|\__,_|\__|_.__/ \___/ \__|
7
+ #
8
+ # __________ ___ ___________ _____ ___
9
+ # / _/_ __/ / _ )/ __/_ __| \/ / _ | / _ \
10
+ # _/ / / / / _ / _/ / / \ / __ |/ , _/
11
+ # /___/ /_/ /____/___/ /_/ /_/_/ |_/_/|_|
12
+ #
13
+ # SOFT PILLOW VERZIÓ
14
+ # HF - Streamlit verzió
15
+
16
+ import streamlit as st
17
+ import time
18
+ from openai import OpenAI
19
+ import os
20
+
21
+ # Get secrets from environment variables
22
+ api_key = os.environ.get('OPENAI_API_KEY')
23
+ assistant_id = os.environ.get('ASSISTANT_ID')
24
+
25
+ # Initialize OpenAI client
26
+ client = OpenAI(api_key=api_key)
27
+
28
+ # Center-align the image and title
29
+ st.markdown(
30
+ """
31
+ <div style="text-align: center">
32
+ <img src="https://huggingface.co/spaces/InfoBetyar/RAG-Chat/resolve/main/puhaparna.webp" width="300">
33
+ </div>
34
+ """,
35
+ unsafe_allow_html=True
36
+ )
37
+
38
+ # Streamlit App Title
39
+ st.title("IT Betyár - ChatBot")
40
+
41
+ # Verify credentials are loaded
42
+ if not api_key or not assistant_id:
43
+ st.error("Please set up OPENAI_API_KEY and ASSISTANT_ID in your Hugging Face Space secrets.")
44
+ st.stop()
45
+
46
+ # Initialize messages in session state if not present
47
+ if "messages" not in st.session_state:
48
+ st.session_state.messages = []
49
+
50
+ # Display existing messages
51
+ for message in st.session_state.messages:
52
+ with st.chat_message(message["role"]):
53
+ st.write(message["content"])
54
+
55
+ # Chat input
56
+ if prompt := st.chat_input("Type your message..."):
57
+ # Display user message
58
+ with st.chat_message("user"):
59
+ st.write(prompt)
60
+
61
+ # Add user message to state
62
+ st.session_state.messages.append({"role": "user", "content": prompt})
63
+
64
+ # Get assistant response
65
+ with st.chat_message("assistant"):
66
+ with st.spinner("Thinking..."):
67
+ try:
68
+ # Create thread and send message
69
+ thread = client.beta.threads.create()
70
+ client.beta.threads.messages.create(
71
+ thread_id=thread.id,
72
+ role="user",
73
+ content=prompt
74
+ )
75
+
76
+ # Run assistant
77
+ run = client.beta.threads.runs.create(
78
+ thread_id=thread.id,
79
+ assistant_id=assistant_id
80
+ )
81
+
82
+ # Wait for completion
83
+ while run.status != "completed":
84
+ time.sleep(0.5)
85
+ run = client.beta.threads.runs.retrieve(
86
+ thread_id=thread.id,
87
+ run_id=run.id
88
+ )
89
+
90
+ # Get and display response
91
+ messages = client.beta.threads.messages.list(thread_id=thread.id)
92
+ response = messages.data[0].content[0].text.value
93
+ cleaned_response = response.split("【")[0]
94
+ st.write(cleaned_response)
95
+
96
+ # Add assistant response to state
97
+ st.session_state.messages.append(
98
+ {"role": "assistant", "content": cleaned_response}
99
+ )
100
+
101
+ # Clean up thread
102
+ client.beta.threads.delete(thread.id)
103
+
104
+ except Exception as e:
105
+ st.error(f"An error occurred: {str(e)}")
106
+
107
+
108
+ # Teszt üzenetek:
109
+ #-------------------------------------
110
+ # What's the check in time?
111
+ # What are the check-in and check-out times?
112
+ # Are there any local restaurants you'd suggest?
113
+ # What's the Wi-Fi password for the property?
114
+ # What's the pin for the lockbox?
115
+ # What is the check-out procedure?
116
+ # What is the manager's name? --> NEM kell tudja!
117
+