Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,44 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from huggingface_hub import Repository
|
| 3 |
import openai
|
| 4 |
-
import os
|
| 5 |
import uuid
|
| 6 |
import time
|
| 7 |
|
| 8 |
-
|
| 9 |
-
repo = Repository(
|
| 10 |
-
local_dir="Test_csv", # Local directory to clone the repository
|
| 11 |
-
repo_type="dataset",
|
| 12 |
-
clone_from="Anne31415/Test_csv",
|
| 13 |
-
token=os.environ["HUB_TOKEN"] # Token for authentication
|
| 14 |
-
)
|
| 15 |
-
repo.git_pull() # Pull the latest changes (if any)
|
| 16 |
|
| 17 |
-
file_path = "Test_csv/Basic_Info_GrunddatenKH141123.csv"
|
| 18 |
|
|
|
|
| 19 |
|
| 20 |
-
from openai import OpenAI
|
| 21 |
-
client = OpenAI()
|
| 22 |
|
| 23 |
-
# Select the preferred model
|
| 24 |
MODEL = "gpt-4-1106-preview"
|
| 25 |
|
| 26 |
-
|
|
|
|
| 27 |
st.session_state.session_id = str(uuid.uuid4())
|
| 28 |
|
| 29 |
-
if "run" not in st.session_state:
|
| 30 |
st.session_state.run = {"status": None}
|
| 31 |
|
| 32 |
-
if "messages" not in st.session_state:
|
| 33 |
st.session_state.messages = []
|
| 34 |
|
| 35 |
-
if "retry_error" not in st.session_state:
|
| 36 |
st.session_state.retry_error = 0
|
| 37 |
|
| 38 |
-
|
| 39 |
-
st.
|
| 40 |
-
st.sidebar.
|
| 41 |
-
st.sidebar.
|
| 42 |
-
st.sidebar.markdown("
|
| 43 |
-
st.sidebar.markdown("Current Version: 0.0.3")
|
| 44 |
-
st.sidebar.markdown("Using gpt-4-1106-preview API")
|
| 45 |
st.sidebar.markdown(st.session_state.session_id)
|
| 46 |
st.sidebar.divider()
|
|
|
|
|
|
|
| 47 |
|
| 48 |
if "assistant" not in st.session_state:
|
| 49 |
-
openai.api_key =
|
| 50 |
-
|
| 51 |
# Load the previously created assistant
|
| 52 |
-
st.session_state.assistant = openai.beta.assistants.retrieve(
|
| 53 |
|
| 54 |
# Create a new thread for this session
|
| 55 |
st.session_state.thread = client.beta.threads.create(
|
|
@@ -57,8 +46,7 @@ if "assistant" not in st.session_state:
|
|
| 57 |
'session_id': st.session_state.session_id,
|
| 58 |
}
|
| 59 |
)
|
| 60 |
-
|
| 61 |
-
|
| 62 |
# If the run is completed, display the messages
|
| 63 |
elif hasattr(st.session_state.run, 'status') and st.session_state.run.status == "completed":
|
| 64 |
# Retrieve the list of messages
|
|
@@ -66,40 +54,15 @@ elif hasattr(st.session_state.run, 'status') and st.session_state.run.status ==
|
|
| 66 |
thread_id=st.session_state.thread.id
|
| 67 |
)
|
| 68 |
|
| 69 |
-
# Display
|
| 70 |
-
for thread_message in st.session_state.messages.data:
|
| 71 |
-
for message_content in thread_message.content:
|
| 72 |
-
# Access the actual text content
|
| 73 |
-
message_content = message_content.text
|
| 74 |
-
annotations = message_content.annotations
|
| 75 |
-
citations = []
|
| 76 |
-
|
| 77 |
-
# Iterate over the annotations and add footnotes
|
| 78 |
-
for index, annotation in enumerate(annotations):
|
| 79 |
-
# Replace the text with a footnote
|
| 80 |
-
message_content.value = message_content.value.replace(annotation.text, f' [{index}]')
|
| 81 |
-
|
| 82 |
-
# Gather citations based on annotation attributes
|
| 83 |
-
if (file_citation := getattr(annotation, 'file_citation', None)):
|
| 84 |
-
cited_file = client.files.retrieve(file_citation.file_id)
|
| 85 |
-
citations.append(f'[{index}] {file_citation.quote} from {cited_file.filename}')
|
| 86 |
-
elif (file_path := getattr(annotation, 'file_path', None)):
|
| 87 |
-
cited_file = client.files.retrieve(file_path.file_id)
|
| 88 |
-
citations.append(f'[{index}] Click <here> to download {cited_file.filename}')
|
| 89 |
-
# Note: File download functionality not implemented above for brevity
|
| 90 |
-
|
| 91 |
-
# Add footnotes to the end of the message before displaying to user
|
| 92 |
-
message_content.value += '\n' + '\n'.join(citations)
|
| 93 |
-
|
| 94 |
-
# Display messages
|
| 95 |
for message in reversed(st.session_state.messages.data):
|
| 96 |
if message.role in ["user", "assistant"]:
|
| 97 |
with st.chat_message(message.role):
|
| 98 |
for content_part in message.content:
|
| 99 |
message_text = content_part.text.value
|
| 100 |
-
st.markdown(message_text)
|
| 101 |
|
| 102 |
-
if prompt := st.chat_input("
|
| 103 |
with st.chat_message('user'):
|
| 104 |
st.write(prompt)
|
| 105 |
|
|
@@ -110,7 +73,7 @@ if prompt := st.chat_input("How can I help you?"):
|
|
| 110 |
content=prompt
|
| 111 |
)
|
| 112 |
|
| 113 |
-
# Do a run to process the messages in the thread
|
| 114 |
st.session_state.run = client.beta.threads.runs.create(
|
| 115 |
thread_id=st.session_state.thread.id,
|
| 116 |
assistant_id=st.session_state.assistant.id,
|
|
@@ -118,7 +81,7 @@ if prompt := st.chat_input("How can I help you?"):
|
|
| 118 |
if st.session_state.retry_error < 3:
|
| 119 |
time.sleep(1) # Wait 1 second before checking run status
|
| 120 |
st.rerun()
|
| 121 |
-
|
| 122 |
# Check if 'run' object has 'status' attribute
|
| 123 |
if hasattr(st.session_state.run, 'status'):
|
| 124 |
# Handle the 'running' status
|
|
@@ -150,6 +113,3 @@ if hasattr(st.session_state.run, 'status'):
|
|
| 150 |
if st.session_state.retry_error < 3:
|
| 151 |
time.sleep(3)
|
| 152 |
st.rerun()
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
|
|
|
| 1 |
+
# Importing required packages
|
| 2 |
import streamlit as st
|
|
|
|
| 3 |
import openai
|
|
|
|
| 4 |
import uuid
|
| 5 |
import time
|
| 6 |
|
| 7 |
+
from openai import OpenAI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
| 9 |
|
| 10 |
+
client = OpenAI(api_key="Hier deienen OPENAI-API-KEY einfügen")
|
| 11 |
|
|
|
|
|
|
|
| 12 |
|
|
|
|
| 13 |
MODEL = "gpt-4-1106-preview"
|
| 14 |
|
| 15 |
+
|
| 16 |
+
if "session_id" not in st.session_state:
|
| 17 |
st.session_state.session_id = str(uuid.uuid4())
|
| 18 |
|
| 19 |
+
if "run" not in st.session_state:
|
| 20 |
st.session_state.run = {"status": None}
|
| 21 |
|
| 22 |
+
if "messages" not in st.session_state:
|
| 23 |
st.session_state.messages = []
|
| 24 |
|
| 25 |
+
if "retry_error" not in st.session_state:
|
| 26 |
st.session_state.retry_error = 0
|
| 27 |
|
| 28 |
+
st.set_page_config(page_title="OpenAI - Assistent API")
|
| 29 |
+
st.sidebar.title("OpenAI - Assistant API🤖")
|
| 30 |
+
st.sidebar.markdown("**Model:** gpt-4-1106-preview API")
|
| 31 |
+
st.sidebar.markdown("**Version:** 1.1")
|
| 32 |
+
st.sidebar.markdown("**Session-ID:**")
|
|
|
|
|
|
|
| 33 |
st.sidebar.markdown(st.session_state.session_id)
|
| 34 |
st.sidebar.divider()
|
| 35 |
+
st.title("Assitent-Name")
|
| 36 |
+
st.text("Beschreibung")
|
| 37 |
|
| 38 |
if "assistant" not in st.session_state:
|
| 39 |
+
openai.api_key = "Hier deinen OPENAI-API-KEY einfügen"
|
|
|
|
| 40 |
# Load the previously created assistant
|
| 41 |
+
st.session_state.assistant = openai.beta.assistants.retrieve("Hier deine ASSISTENT-ID Einfügen")
|
| 42 |
|
| 43 |
# Create a new thread for this session
|
| 44 |
st.session_state.thread = client.beta.threads.create(
|
|
|
|
| 46 |
'session_id': st.session_state.session_id,
|
| 47 |
}
|
| 48 |
)
|
| 49 |
+
|
|
|
|
| 50 |
# If the run is completed, display the messages
|
| 51 |
elif hasattr(st.session_state.run, 'status') and st.session_state.run.status == "completed":
|
| 52 |
# Retrieve the list of messages
|
|
|
|
| 54 |
thread_id=st.session_state.thread.id
|
| 55 |
)
|
| 56 |
|
| 57 |
+
# Display messages
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
for message in reversed(st.session_state.messages.data):
|
| 59 |
if message.role in ["user", "assistant"]:
|
| 60 |
with st.chat_message(message.role):
|
| 61 |
for content_part in message.content:
|
| 62 |
message_text = content_part.text.value
|
| 63 |
+
st.markdown(message_text)
|
| 64 |
|
| 65 |
+
if prompt := st.chat_input("Wie kann ich dir helfen?"):
|
| 66 |
with st.chat_message('user'):
|
| 67 |
st.write(prompt)
|
| 68 |
|
|
|
|
| 73 |
content=prompt
|
| 74 |
)
|
| 75 |
|
| 76 |
+
# Do a run to process the messages in the thread
|
| 77 |
st.session_state.run = client.beta.threads.runs.create(
|
| 78 |
thread_id=st.session_state.thread.id,
|
| 79 |
assistant_id=st.session_state.assistant.id,
|
|
|
|
| 81 |
if st.session_state.retry_error < 3:
|
| 82 |
time.sleep(1) # Wait 1 second before checking run status
|
| 83 |
st.rerun()
|
| 84 |
+
|
| 85 |
# Check if 'run' object has 'status' attribute
|
| 86 |
if hasattr(st.session_state.run, 'status'):
|
| 87 |
# Handle the 'running' status
|
|
|
|
| 113 |
if st.session_state.retry_error < 3:
|
| 114 |
time.sleep(3)
|
| 115 |
st.rerun()
|
|
|
|
|
|
|
|
|