SVashishta1
commited on
Commit
Β·
a72e3c3
1
Parent(s):
902313a
Error Fix
Browse files
app.py
CHANGED
|
@@ -715,53 +715,34 @@ def process_file_upload(files):
|
|
| 715 |
|
| 716 |
def list_documents():
|
| 717 |
"""List all indexed documents"""
|
| 718 |
-
info_list = []
|
| 719 |
-
|
| 720 |
-
# Check for CSV data
|
| 721 |
try:
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
tables = cursor.fetchall()
|
| 726 |
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
# Get column info
|
| 731 |
-
cursor.execute(f"PRAGMA table_info({table[0]});")
|
| 732 |
-
columns = [col[1] for col in cursor.fetchall()]
|
| 733 |
-
|
| 734 |
-
# Get row count
|
| 735 |
-
cursor.execute(f"SELECT COUNT(*) FROM {table[0]};")
|
| 736 |
-
row_count = cursor.fetchone()[0]
|
| 737 |
-
|
| 738 |
-
info_list.append(f"- {table[0]} ({row_count:,} rows, {len(columns)} columns)")
|
| 739 |
|
| 740 |
-
|
| 741 |
except Exception as e:
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
# Check for indexed documents
|
| 745 |
-
docs = document_assistant.get_all_documents()
|
| 746 |
-
if docs:
|
| 747 |
-
info_list.append("\nπ Indexed Documents:")
|
| 748 |
-
for doc in docs:
|
| 749 |
-
info_list.append(f"- {doc['filename']} (ID: {doc['id']})")
|
| 750 |
-
|
| 751 |
-
if not info_list:
|
| 752 |
-
return "No data or documents loaded yet"
|
| 753 |
-
|
| 754 |
-
return "\n".join(info_list)
|
| 755 |
|
| 756 |
def clear_context():
|
| 757 |
-
"""Clear the current context
|
| 758 |
global current_context
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 765 |
|
| 766 |
def process_voice_input(audio_path):
|
| 767 |
"""Process voice input and return transcribed text"""
|
|
@@ -780,7 +761,7 @@ def process_voice_input(audio_path):
|
|
| 780 |
# Recognize speech using Google Speech Recognition
|
| 781 |
text = r.recognize_google(audio_data)
|
| 782 |
|
| 783 |
-
|
| 784 |
except sr.UnknownValueError:
|
| 785 |
return "Could not understand audio"
|
| 786 |
except sr.RequestError as e:
|
|
@@ -825,39 +806,47 @@ def text_to_speech_output(text):
|
|
| 825 |
|
| 826 |
def create_test_visualization():
|
| 827 |
"""Create a test visualization to verify plotting works"""
|
| 828 |
-
|
| 829 |
-
|
| 830 |
-
|
| 831 |
-
|
| 832 |
-
|
| 833 |
-
|
| 834 |
-
|
| 835 |
-
|
| 836 |
-
|
| 837 |
-
|
| 838 |
-
|
| 839 |
-
|
| 840 |
-
|
| 841 |
-
|
| 842 |
-
|
| 843 |
-
|
| 844 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 845 |
|
| 846 |
def create_test_html_visualization():
|
| 847 |
"""Create a test HTML visualization to verify plotting works"""
|
| 848 |
-
|
| 849 |
-
|
| 850 |
-
|
| 851 |
-
|
| 852 |
-
|
| 853 |
-
|
| 854 |
-
|
| 855 |
-
|
| 856 |
-
|
| 857 |
-
|
| 858 |
-
|
| 859 |
-
|
| 860 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 861 |
|
| 862 |
def flush_databases():
|
| 863 |
"""Flush ChromaDB and SQLite databases"""
|
|
@@ -885,8 +874,11 @@ def flush_databases():
|
|
| 885 |
|
| 886 |
# Flush ChromaDB by resetting the document assistant
|
| 887 |
try:
|
| 888 |
-
document_assistant.reset_database()
|
| 889 |
-
|
|
|
|
|
|
|
|
|
|
| 890 |
except Exception as e:
|
| 891 |
result.append(f"β Error clearing ChromaDB: {str(e)}")
|
| 892 |
|
|
@@ -1035,8 +1027,11 @@ with gr.Blocks(title="AI Document Analysis & Voice Assistant") as demo:
|
|
| 1035 |
save_btn = gr.Button("Save Settings")
|
| 1036 |
|
| 1037 |
def save_settings(key):
|
| 1038 |
-
|
| 1039 |
-
|
|
|
|
|
|
|
|
|
|
| 1040 |
|
| 1041 |
save_btn.click(
|
| 1042 |
save_settings,
|
|
|
|
| 715 |
|
| 716 |
def list_documents():
|
| 717 |
"""List all indexed documents"""
|
|
|
|
|
|
|
|
|
|
| 718 |
try:
|
| 719 |
+
docs = document_assistant.get_all_documents()
|
| 720 |
+
if not docs:
|
| 721 |
+
return "No documents indexed yet."
|
|
|
|
| 722 |
|
| 723 |
+
result = "Indexed Documents:\n\n"
|
| 724 |
+
for doc in docs:
|
| 725 |
+
result += f"- {doc['filename']} ({doc['file_type']})\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 726 |
|
| 727 |
+
return result
|
| 728 |
except Exception as e:
|
| 729 |
+
return f"Error listing documents: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 730 |
|
| 731 |
def clear_context():
|
| 732 |
+
"""Clear the current context"""
|
| 733 |
global current_context
|
| 734 |
+
|
| 735 |
+
try:
|
| 736 |
+
# Reset the context
|
| 737 |
+
current_context = {
|
| 738 |
+
"file_type": None,
|
| 739 |
+
"file_name": None,
|
| 740 |
+
"table_name": None
|
| 741 |
+
}
|
| 742 |
+
|
| 743 |
+
return [{"role": "assistant", "content": "Context cleared. You can now upload new documents or CSV files."}]
|
| 744 |
+
except Exception as e:
|
| 745 |
+
return [{"role": "assistant", "content": f"Error clearing context: {str(e)}"}]
|
| 746 |
|
| 747 |
def process_voice_input(audio_path):
|
| 748 |
"""Process voice input and return transcribed text"""
|
|
|
|
| 761 |
# Recognize speech using Google Speech Recognition
|
| 762 |
text = r.recognize_google(audio_data)
|
| 763 |
|
| 764 |
+
return text
|
| 765 |
except sr.UnknownValueError:
|
| 766 |
return "Could not understand audio"
|
| 767 |
except sr.RequestError as e:
|
|
|
|
| 806 |
|
| 807 |
def create_test_visualization():
|
| 808 |
"""Create a test visualization to verify plotting works"""
|
| 809 |
+
try:
|
| 810 |
+
# Create sample data
|
| 811 |
+
data = pd.DataFrame({
|
| 812 |
+
'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
|
| 813 |
+
'Value': [10, 15, 13, 17, 20, 25]
|
| 814 |
+
})
|
| 815 |
+
|
| 816 |
+
# Create a simple bar chart
|
| 817 |
+
fig = px.bar(data, x='Month', y='Value', title='Test Visualization')
|
| 818 |
+
|
| 819 |
+
# Configure the figure
|
| 820 |
+
fig.update_layout(
|
| 821 |
+
autosize=True,
|
| 822 |
+
width=800,
|
| 823 |
+
height=500
|
| 824 |
+
)
|
| 825 |
+
|
| 826 |
+
return fig
|
| 827 |
+
except Exception as e:
|
| 828 |
+
print(f"Error creating test visualization: {str(e)}")
|
| 829 |
+
return None
|
| 830 |
|
| 831 |
def create_test_html_visualization():
|
| 832 |
"""Create a test HTML visualization to verify plotting works"""
|
| 833 |
+
try:
|
| 834 |
+
# Create sample data
|
| 835 |
+
data = pd.DataFrame({
|
| 836 |
+
'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
|
| 837 |
+
'Value': [10, 15, 13, 17, 20, 25]
|
| 838 |
+
})
|
| 839 |
+
|
| 840 |
+
# Create a simple bar chart
|
| 841 |
+
fig = px.bar(data, x='Month', y='Value', title='Test Visualization')
|
| 842 |
+
|
| 843 |
+
# Convert to HTML
|
| 844 |
+
html = pio.to_html(fig, full_html=False)
|
| 845 |
+
|
| 846 |
+
return html
|
| 847 |
+
except Exception as e:
|
| 848 |
+
print(f"Error creating test HTML visualization: {str(e)}")
|
| 849 |
+
return None
|
| 850 |
|
| 851 |
def flush_databases():
|
| 852 |
"""Flush ChromaDB and SQLite databases"""
|
|
|
|
| 874 |
|
| 875 |
# Flush ChromaDB by resetting the document assistant
|
| 876 |
try:
|
| 877 |
+
success = document_assistant.reset_database()
|
| 878 |
+
if success:
|
| 879 |
+
result.append("β
ChromaDB cleared successfully")
|
| 880 |
+
else:
|
| 881 |
+
result.append("β οΈ ChromaDB reset may not have been complete")
|
| 882 |
except Exception as e:
|
| 883 |
result.append(f"β Error clearing ChromaDB: {str(e)}")
|
| 884 |
|
|
|
|
| 1027 |
save_btn = gr.Button("Save Settings")
|
| 1028 |
|
| 1029 |
def save_settings(key):
|
| 1030 |
+
try:
|
| 1031 |
+
os.environ["GROQ_API_KEY"] = key
|
| 1032 |
+
return "Settings saved!"
|
| 1033 |
+
except Exception as e:
|
| 1034 |
+
return f"Error saving settings: {str(e)}"
|
| 1035 |
|
| 1036 |
save_btn.click(
|
| 1037 |
save_settings,
|