Spaces:
Sleeping
Sleeping
Create App.py
Browse files
App.py
ADDED
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
from datasets import load_dataset
|
4 |
+
from huggingface_hub import InferenceClient
|
5 |
+
|
6 |
+
# Get the API key from the environment variable
|
7 |
+
api_key = os.getenv("HF_API_KEY")
|
8 |
+
client = InferenceClient(api_key=api_key)
|
9 |
+
|
10 |
+
# Load the dataset
|
11 |
+
dataset = load_dataset("andreska/Adrega62Manual", split="test")
|
12 |
+
|
13 |
+
# Function to read the content from the dataset
|
14 |
+
def read_dataset(dataset):
|
15 |
+
text = []
|
16 |
+
for item in dataset:
|
17 |
+
text.append(item['text'])
|
18 |
+
return "\n".join(text)
|
19 |
+
|
20 |
+
context = read_dataset(dataset)
|
21 |
+
|
22 |
+
# Inject custom CSS
|
23 |
+
st.markdown(
|
24 |
+
"""
|
25 |
+
<style>
|
26 |
+
.scrollable-div {
|
27 |
+
height: 390px;
|
28 |
+
width: 100%;
|
29 |
+
overflow-y: auto;
|
30 |
+
padding: 10px;
|
31 |
+
border: 1px solid #ccc;
|
32 |
+
}
|
33 |
+
|
34 |
+
.block-container {
|
35 |
+
padding-top: 3rem;
|
36 |
+
padding-bottom: 0rem;
|
37 |
+
padding-left: 5rem;
|
38 |
+
padding-right: 5rem;
|
39 |
+
}
|
40 |
+
.stButton > button {
|
41 |
+
height: 27px;
|
42 |
+
background-color: #f85900;
|
43 |
+
color: white;
|
44 |
+
border: none;
|
45 |
+
transition: all 0.3s ease;
|
46 |
+
}
|
47 |
+
.stButton > button:hover {
|
48 |
+
background-color: #F97A33;
|
49 |
+
color: white;
|
50 |
+
border: none;
|
51 |
+
opacity: 1;
|
52 |
+
}
|
53 |
+
.stButton > button:active {
|
54 |
+
background-color: #B84200;
|
55 |
+
color: white;
|
56 |
+
opacity: 1;
|
57 |
+
}
|
58 |
+
</style>
|
59 |
+
""",
|
60 |
+
unsafe_allow_html=True
|
61 |
+
)
|
62 |
+
|
63 |
+
placeholder = st.empty()
|
64 |
+
# Define the placeholder globally (outside columns)
|
65 |
+
if st.session_state and 'conversation' in st.session_state:
|
66 |
+
placeholder.markdown(f'<div class="scrollable-div">{st.session_state.conversation}</div>', unsafe_allow_html=True)
|
67 |
+
else:
|
68 |
+
placeholder.markdown(f'<div class="scrollable-div"><p>Welcome! I am your Adrega AI assistant</p></div>', unsafe_allow_html=True)
|
69 |
+
|
70 |
+
def handle_submit():
|
71 |
+
user_input = st.session_state.user_input
|
72 |
+
|
73 |
+
if user_input:
|
74 |
+
messages = [
|
75 |
+
{"role": "system", "content": f"Context: {context}"},
|
76 |
+
{"role": "user", "content": user_input}
|
77 |
+
]
|
78 |
+
|
79 |
+
# List of models to try in order
|
80 |
+
models = [
|
81 |
+
#Uncomment all to have backup models in case one doesn't work. Notice it will use extra inference calls for each non-working model.
|
82 |
+
#"mistralai/Mistral-7B-Instruct-v0.2", #not enough tokens for Adrega. 32k while 35k is passed. But else works.
|
83 |
+
"Qwen/Qwen2.5-72B-Instruct", #Secondary model
|
84 |
+
#"Qwen/Qwen2.5-Coder-32B-Instruct", # Primary model
|
85 |
+
]
|
86 |
+
|
87 |
+
for model in models:
|
88 |
+
try:
|
89 |
+
response = client.chat.completions.create(
|
90 |
+
model=model,
|
91 |
+
messages=messages,
|
92 |
+
max_tokens=1000,
|
93 |
+
stream=True
|
94 |
+
)
|
95 |
+
|
96 |
+
answer = ""
|
97 |
+
for chunk in response:
|
98 |
+
answer += chunk['choices'][0]['delta']['content']
|
99 |
+
placeholder.markdown(f'<div class="scrollable-div"><p>{answer}</p></div>', unsafe_allow_html=True)
|
100 |
+
|
101 |
+
st.session_state.conversation = f"<p>{answer}</p>"
|
102 |
+
placeholder.markdown(f'<div class="scrollable-div">{st.session_state.conversation}</div>', unsafe_allow_html=True)
|
103 |
+
break # If successful, exit the loop
|
104 |
+
|
105 |
+
except Exception as e:
|
106 |
+
if "422" in str(e): # Service Unavailable error
|
107 |
+
if model == models[-1]: # If this was the last model to try
|
108 |
+
error_message = f"422 An error occurred: {str(e)}"
|
109 |
+
#error_message = "All models are currently unavailable. Please try again later."
|
110 |
+
placeholder.markdown(f'<div class="scrollable-div"><p>{error_message}</p></div>', unsafe_allow_html=True)
|
111 |
+
continue # Try the next model
|
112 |
+
else:
|
113 |
+
# Handle other types of errors
|
114 |
+
error_message = f"An error occurred: {str(e)}"
|
115 |
+
placeholder.markdown(f'<div class="scrollable-div"><p>{error_message}</p></div>', unsafe_allow_html=True)
|
116 |
+
break
|
117 |
+
|
118 |
+
st.text_input('Ask me a question', key='user_input', on_change=handle_submit)
|
119 |
+
|
120 |
+
if st.button("Ask"):
|
121 |
+
handle_submit()
|