Spaces:
Sleeping
Sleeping
| import requests | |
| from frontend.app import common_functions | |
| import streamlit as st | |
| API_BASE_URL = "http://localhost:8000/knowledge-base" | |
| def upsert_data(): | |
| """ | |
| Displays a form to upsert data into the Pinecone database. | |
| Features: | |
| - Users can provide 'Input', 'Output', and 'Instruction'. | |
| - Displays appropriate success or error messages. | |
| - Improved error handling with detailed feedback. | |
| """ | |
| st.subheader("Enter the data to upsert") | |
| with st.form("upsert_form"): | |
| # Input Fields | |
| input_text = st.text_area("Input", placeholder="Enter input text here...", height=150) | |
| output_text = st.text_area("Output", placeholder="Enter output text here...", height=150) | |
| instruction_text = st.text_input("Instruction", placeholder="Provide guidance for the data...") | |
| upsert_submit = st.form_submit_button("π Upsert Data") | |
| if upsert_submit: | |
| # β Validation Check | |
| if not input_text.strip() or not output_text.strip() or not instruction_text.strip(): | |
| st.error("β All fields are required. Please fill out each section before submitting.") | |
| return | |
| # β Payload Creation | |
| payload = { | |
| "data": [ | |
| { | |
| "input": input_text.strip(), | |
| "output": output_text.strip(), | |
| "instruction": instruction_text.strip() | |
| } | |
| ] | |
| } | |
| # API Call | |
| with st.spinner("β³ Processing your data..."): | |
| try: | |
| response = requests.post(f"{API_BASE_URL}/upsert-data", json=payload) | |
| response_data = response.json() | |
| if response.status_code == 200: | |
| st.success(f"β Data successfully upserted: {response_data.get('message', 'Success')}") | |
| st.toast("π Upsert successful!") | |
| elif response.status_code == 400: | |
| st.warning(f"β οΈ Bad Request: {response_data.get('detail', 'Check your input data.')}") | |
| elif response.status_code == 500: | |
| st.error("β Internal Server Error. Please try again later.") | |
| else: | |
| st.error(f"β Unexpected error: {response_data.get('detail', 'Unknown issue occurred.')}") | |
| except requests.exceptions.RequestException as e: | |
| st.error(f"β Network error: {e}") | |
| def delete_records(): | |
| """ | |
| Displays a form to delete records from the Pinecone database. | |
| Features: | |
| - Users can input comma-separated IDs for deletion. | |
| - Includes validation checks for empty or malformed IDs. | |
| - Enhanced error handling for better user feedback. | |
| """ | |
| st.subheader("Enter id to delete") | |
| with st.form("delete_form"): | |
| ids_input = st.text_area("IDs to Delete", placeholder="Enter IDs separated by commas (e.g., id_123, id_456)") | |
| ids_to_delete = [id.strip() for id in ids_input.split(",") if id.strip()] | |
| delete_submit = st.form_submit_button("ποΈ Delete Records") | |
| if delete_submit: | |
| # β Validation Check | |
| if not ids_to_delete: | |
| st.error("β Please provide at least one valid ID.") | |
| return | |
| # π Confirmation Prompt for Safety | |
| if not st.confirm("Are you sure you want to delete the selected records? This action is irreversible."): | |
| st.info("β Deletion canceled.") | |
| return | |
| # β Payload Creation | |
| payload = {"ids_to_delete": ids_to_delete} | |
| # β API Call with Improved Error Handling | |
| with st.spinner("β³ Deleting records..."): | |
| try: | |
| response = requests.post(f"{API_BASE_URL}/delete-records", json=payload) | |
| response_data = response.json() | |
| if response.status_code == 200: | |
| st.success(f"β {response_data.get('message', 'Records successfully deleted.')}") | |
| st.toast("π― Deletion successful!") | |
| elif response.status_code == 400: | |
| st.warning(f"β οΈ Bad Request: {response_data.get('detail', 'Check the provided IDs.')}") | |
| elif response.status_code == 404: | |
| st.warning("β οΈ No matching records found. Please verify the provided IDs.") | |
| elif response.status_code == 500: | |
| st.error("β Internal Server Error. Please try again later.") | |
| else: | |
| st.error(f"β Unexpected error: {response_data.get('detail', 'Unknown issue occurred.')}") | |
| except requests.exceptions.RequestException as e: | |
| st.error(f"β Network error: {e}") | |
| def render_metadata_fetch_form(): | |
| """ | |
| Renders a form to fetch metadata based on user concerns. | |
| Features: | |
| - Input validation to ensure meaningful data is provided. | |
| - Displays metadata in a visually appealing format. | |
| - Improved error handling with detailed messages. | |
| """ | |
| st.header("π Fetch Metadata") | |
| with st.form("fetch_metadata_form"): | |
| # π Input Fields | |
| prompt_text = st.text_area( | |
| "Describe Your Concern", | |
| "e.g., I've been feeling anxious lately.", | |
| help="Provide a brief description of your concern to get relevant metadata." | |
| ) | |
| n_result = st.number_input( | |
| "Number of Results", | |
| min_value=1, | |
| value=3, | |
| help="Specify the number of search results you'd like to retrieve." | |
| ) | |
| score_threshold = st.number_input( | |
| "Score Threshold", | |
| min_value=0.0, | |
| max_value=1.0, | |
| value=0.47, | |
| help="Set the minimum relevance score for the results. Higher values ensure more accurate matches." | |
| ) | |
| metadata_submit = st.form_submit_button("π Fetch Metadata") | |
| if metadata_submit: | |
| # β Input Validation | |
| if not prompt_text.strip(): | |
| st.warning("β Please provide a valid concern description.") | |
| return | |
| payload = { | |
| "prompt": prompt_text.strip(), | |
| "n_result": n_result, | |
| "score_threshold": score_threshold | |
| } | |
| # π Enhanced API Request with Better Error Handling | |
| with st.spinner("β³ Fetching metadata..."): | |
| try: | |
| response = requests.post(f"{API_BASE_URL}/fetch-metadata", json=payload) | |
| response.raise_for_status() | |
| metadata = response.json().get('metadata', []) | |
| # β Display Results | |
| if metadata: | |
| st.success(f"β Found {len(metadata)} relevant result(s).") | |
| st.subheader("Search Results") | |
| for index, entry in enumerate(metadata, start=1): | |
| common_functions.typewriter_effect(f"**π§ Question:** {entry.get('question', 'N/A')}",speed=0) | |
| common_functions.typewriter_effect(f"**π¬ Answer:** {entry.get('answer', 'N/A')}",speed=0) | |
| st.markdown(f"**π Score:** `{entry.get('score', 'N/A')}`") | |
| st.markdown(f"**π ID:** `{entry.get('id', 'N/A')}`") | |
| else: | |
| st.info("π€ No relevant data found. Try refining your concern or adjusting the score threshold.") | |
| # Exception Handling for Specific Errors | |
| except requests.exceptions.HTTPError as http_err: | |
| st.error(f"β HTTP Error: {http_err}") | |
| except requests.exceptions.ConnectionError: | |
| st.error("β Network error. Please check your internet connection.") | |
| except requests.exceptions.Timeout: | |
| st.error("β Request timed out. Please try again later.") | |
| except requests.exceptions.RequestException as e: | |
| st.error(f"β Unexpected error: {e}") | |