Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -184,6 +184,190 @@ def save_to_cosmos_db(container, query, response1, response2):
|
|
| 184 |
except Exception as e:
|
| 185 |
st.error(f"An unexpected error occurred: {str(e)}")
|
| 186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
# GitHub functions
|
| 188 |
def download_github_repo(url, local_path):
|
| 189 |
if os.path.exists(local_path):
|
|
@@ -242,6 +426,17 @@ def main():
|
|
| 242 |
if 'cloned_doc' not in st.session_state:
|
| 243 |
st.session_state.cloned_doc = None
|
| 244 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
# Automatic Login
|
| 246 |
if Key:
|
| 247 |
st.session_state.primary_key = Key
|
|
|
|
| 184 |
except Exception as e:
|
| 185 |
st.error(f"An unexpected error occurred: {str(e)}")
|
| 186 |
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
# Add dropdowns for model and database choices
|
| 190 |
+
def search_glossary(query):
|
| 191 |
+
st.markdown(f"### ๐ Search Glossary for: `{query}`")
|
| 192 |
+
|
| 193 |
+
# Dropdown for model selection
|
| 194 |
+
model_options = ['mistralai/Mixtral-8x7B-Instruct-v0.1', 'mistralai/Mistral-7B-Instruct-v0.2', 'google/gemma-7b-it', 'None']
|
| 195 |
+
model_choice = st.selectbox('๐ง Select LLM Model', options=model_options, index=1)
|
| 196 |
+
|
| 197 |
+
# Dropdown for database selection
|
| 198 |
+
database_options = ['Semantic Search', 'Arxiv Search - Latest - (EXPERIMENTAL)']
|
| 199 |
+
database_choice = st.selectbox('๐ Select Database', options=database_options, index=0)
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
# Run Button with Emoji
|
| 204 |
+
#if st.button("๐ Run"):
|
| 205 |
+
|
| 206 |
+
# ๐ต๏ธโโ๏ธ Searching the glossary for: query
|
| 207 |
+
all_results = ""
|
| 208 |
+
st.markdown(f"- {query}")
|
| 209 |
+
|
| 210 |
+
# ๐ ArXiv RAG researcher expert ~-<>-~ Paper Summary & Ask LLM
|
| 211 |
+
#database_choice Literal['Semantic Search', 'Arxiv Search - Latest - (EXPERIMENTAL)'] Default: "Semantic Search"
|
| 212 |
+
#llm_model_picked Literal['mistralai/Mixtral-8x7B-Instruct-v0.1', 'mistralai/Mistral-7B-Instruct-v0.2', 'google/gemma-7b-it', 'None'] Default: "mistralai/Mistral-7B-Instruct-v0.2"
|
| 213 |
+
client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
# ๐ ArXiv RAG researcher expert ~-<>-~ Paper Summary & Ask LLM - api_name: /ask_llm
|
| 217 |
+
result = client.predict(
|
| 218 |
+
prompt=query,
|
| 219 |
+
llm_model_picked="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 220 |
+
stream_outputs=True,
|
| 221 |
+
api_name="/ask_llm"
|
| 222 |
+
)
|
| 223 |
+
st.markdown(result)
|
| 224 |
+
st.code(result, language="python", line_numbers=True)
|
| 225 |
+
|
| 226 |
+
# ๐ ArXiv RAG researcher expert ~-<>-~ Paper Summary & Ask LLM - api_name: /ask_llm
|
| 227 |
+
result2 = client.predict(
|
| 228 |
+
prompt=query,
|
| 229 |
+
llm_model_picked="mistralai/Mistral-7B-Instruct-v0.2",
|
| 230 |
+
stream_outputs=True,
|
| 231 |
+
api_name="/ask_llm"
|
| 232 |
+
)
|
| 233 |
+
st.markdown(result2)
|
| 234 |
+
st.code(result2, language="python", line_numbers=True)
|
| 235 |
+
|
| 236 |
+
# ๐ ArXiv RAG researcher expert ~-<>-~ Paper Summary & Ask LLM - api_name: /ask_llm
|
| 237 |
+
result3 = client.predict(
|
| 238 |
+
prompt=query,
|
| 239 |
+
llm_model_picked="google/gemma-7b-it",
|
| 240 |
+
stream_outputs=True,
|
| 241 |
+
api_name="/ask_llm"
|
| 242 |
+
)
|
| 243 |
+
st.markdown(result3)
|
| 244 |
+
st.code(result3, language="python", line_numbers=True)
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
# ๐ ArXiv RAG researcher expert ~-<>-~ Paper Summary & Ask LLM - api_name: /update_with_rag_md
|
| 248 |
+
response2 = client.predict(
|
| 249 |
+
message=query, # str in 'parameter_13' Textbox component
|
| 250 |
+
llm_results_use=10,
|
| 251 |
+
database_choice="Semantic Search",
|
| 252 |
+
llm_model_picked="mistralai/Mistral-7B-Instruct-v0.2",
|
| 253 |
+
api_name="/update_with_rag_md"
|
| 254 |
+
) # update_with_rag_md Returns tuple of 2 elements [0] str The output value that appears in the "value_14" Markdown component. [1] str
|
| 255 |
+
|
| 256 |
+
st.markdown(response2[0])
|
| 257 |
+
st.code(response2[0], language="python", line_numbers=True, wrap_lines=True)
|
| 258 |
+
|
| 259 |
+
st.markdown(response2[1])
|
| 260 |
+
st.code(response2[1], language="python", line_numbers=True, wrap_lines=True)
|
| 261 |
+
|
| 262 |
+
# When saving results, pass the container
|
| 263 |
+
try:
|
| 264 |
+
save_to_cosmos_db(st.session_state.cosmos_container, query, result, result)
|
| 265 |
+
save_to_cosmos_db(st.session_state.cosmos_container, query, result2, result2)
|
| 266 |
+
save_to_cosmos_db(st.session_state.cosmos_container, query, result3, result3)
|
| 267 |
+
save_to_cosmos_db(st.session_state.cosmos_container, query, response2[0], response2[0])
|
| 268 |
+
save_to_cosmos_db(st.session_state.cosmos_container, query, response2[1], response2[1])
|
| 269 |
+
except exceptions.CosmosHttpResponseError as e:
|
| 270 |
+
return False, f"HTTP error occurred: {str(e)} ๐จ"
|
| 271 |
+
except Exception as e:
|
| 272 |
+
return False, f"An unexpected error occurred: {str(e)} ๐ฑ"
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
try:
|
| 276 |
+
# Aggregate hyperlinks and show with emojis
|
| 277 |
+
hyperlinks = extract_hyperlinks([response1, response2])
|
| 278 |
+
st.markdown("### ๐ Aggregated Hyperlinks")
|
| 279 |
+
for link in hyperlinks:
|
| 280 |
+
st.markdown(f"๐ [{link}]({link})")
|
| 281 |
+
|
| 282 |
+
# Show responses in a code format with line numbers
|
| 283 |
+
st.markdown("### ๐ Response Outputs with Line Numbers")
|
| 284 |
+
st.code(f"Response 1: \n{format_with_line_numbers(response1)}\n\nResponse 2: \n{format_with_line_numbers(response2)}", language="json")
|
| 285 |
+
except exceptions.CosmosHttpResponseError as e:
|
| 286 |
+
return False, f"HTTP error occurred: {str(e)} ๐จ"
|
| 287 |
+
except Exception as e:
|
| 288 |
+
return False, f"An unexpected error occurred: {str(e)} ๐ฑ"
|
| 289 |
+
|
| 290 |
+
|
| 291 |
+
|
| 292 |
+
# ๐ค Function to process text input
|
| 293 |
+
def process_text(text_input):
|
| 294 |
+
# ๐ค Processing text inputโtranslating human words into cosmic signals! ๐ก
|
| 295 |
+
if text_input:
|
| 296 |
+
if 'messages' not in st.session_state:
|
| 297 |
+
st.session_state.messages = []
|
| 298 |
+
|
| 299 |
+
st.session_state.messages.append({"role": "user", "content": text_input})
|
| 300 |
+
|
| 301 |
+
with st.chat_message("user"):
|
| 302 |
+
st.markdown(text_input)
|
| 303 |
+
|
| 304 |
+
with st.chat_message("assistant"):
|
| 305 |
+
search_glossary(text_input)
|
| 306 |
+
|
| 307 |
+
# ๐ Function to generate a filename
|
| 308 |
+
def generate_filename(text, file_type):
|
| 309 |
+
# ๐ Generate a filename based on the text input
|
| 310 |
+
safe_text = "".join(c if c.isalnum() or c in (' ', '.', '_') else '_' for c in text)
|
| 311 |
+
safe_text = "_".join(safe_text.strip().split())
|
| 312 |
+
filename = f"{safe_text}.{file_type}"
|
| 313 |
+
return filename
|
| 314 |
+
|
| 315 |
+
# ๐ต๏ธโโ๏ธ Function to extract markdown title
|
| 316 |
+
def extract_markdown_title(content):
|
| 317 |
+
# ๐ต๏ธโโ๏ธ Extracting markdown titleโfinding the headline in the cosmic news! ๐ฐ
|
| 318 |
+
lines = content.splitlines()
|
| 319 |
+
for line in lines:
|
| 320 |
+
if line.startswith('#'):
|
| 321 |
+
return line.lstrip('#').strip()
|
| 322 |
+
return None
|
| 323 |
+
|
| 324 |
+
# ๐พ Function to create and save a file
|
| 325 |
+
def create_and_save_file(content, file_type="md", prompt=None, is_image=False, should_save=True):
|
| 326 |
+
# ๐พ Creating and saving a fileโcapturing cosmic wisdom! ๐
|
| 327 |
+
if not should_save:
|
| 328 |
+
return None
|
| 329 |
+
|
| 330 |
+
# Step 1: Generate filename based on the prompt or content
|
| 331 |
+
filename = generate_filename(prompt if prompt else content, file_type)
|
| 332 |
+
|
| 333 |
+
# Step 2: If it's a markdown file, check if it has a title
|
| 334 |
+
if file_type == "md":
|
| 335 |
+
title_from_content = extract_markdown_title(content)
|
| 336 |
+
if title_from_content:
|
| 337 |
+
filename = generate_filename(title_from_content, file_type)
|
| 338 |
+
|
| 339 |
+
# Step 3: Save the file
|
| 340 |
+
with open(filename, "w", encoding="utf-8") as f:
|
| 341 |
+
if is_image:
|
| 342 |
+
f.write(content)
|
| 343 |
+
else:
|
| 344 |
+
f.write(prompt + "\n\n" + content)
|
| 345 |
+
|
| 346 |
+
return filename
|
| 347 |
+
|
| 348 |
+
# ๐ค Function to insert an auto-generated record
|
| 349 |
+
def insert_auto_generated_record(container):
|
| 350 |
+
# ๐ค Automatically generating a record and inserting it into Cosmos DB!
|
| 351 |
+
try:
|
| 352 |
+
# Generate a unique id
|
| 353 |
+
new_id = generate_unique_id()
|
| 354 |
+
# Create a sample JSON document
|
| 355 |
+
new_doc = {
|
| 356 |
+
'id': new_id,
|
| 357 |
+
'name': f'Sample Name {new_id[:8]}',
|
| 358 |
+
'description': 'This is a sample auto-generated description.',
|
| 359 |
+
'timestamp': datetime.utcnow().isoformat()
|
| 360 |
+
}
|
| 361 |
+
# Insert the document
|
| 362 |
+
container.create_item(body=new_doc)
|
| 363 |
+
return True, f"Record inserted successfully with id: {new_id} ๐"
|
| 364 |
+
except exceptions.CosmosHttpResponseError as e:
|
| 365 |
+
return False, f"HTTP error occurred: {str(e)} ๐จ"
|
| 366 |
+
except Exception as e:
|
| 367 |
+
return False, f"An unexpected error occurred: {str(e)} ๐ฑ"
|
| 368 |
+
|
| 369 |
+
|
| 370 |
+
|
| 371 |
# GitHub functions
|
| 372 |
def download_github_repo(url, local_path):
|
| 373 |
if os.path.exists(local_path):
|
|
|
|
| 426 |
if 'cloned_doc' not in st.session_state:
|
| 427 |
st.session_state.cloned_doc = None
|
| 428 |
|
| 429 |
+
# โ๏ธ q= Run ArXiv search from query parameters
|
| 430 |
+
try:
|
| 431 |
+
query_params = st.query_params
|
| 432 |
+
query = query_params.get('q') or query_params.get('query') or ''
|
| 433 |
+
if query:
|
| 434 |
+
# ๐ต๏ธโโ๏ธ We have a query! Let's process it!
|
| 435 |
+
process_text(query)
|
| 436 |
+
st.stop() # Stop further execution
|
| 437 |
+
except Exception as e:
|
| 438 |
+
st.markdown(' ')
|
| 439 |
+
|
| 440 |
# Automatic Login
|
| 441 |
if Key:
|
| 442 |
st.session_state.primary_key = Key
|