Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -49,151 +49,133 @@ if not os.path.exists(FAISS_INDEX_DIR):
|
|
| 49 |
# Dictionary to store user-specific vectorstores
|
| 50 |
user_vectorstores = {}
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
<div class="tab" data-tab="stack-results">Stack Overflow Results</div>
|
| 171 |
-
<div class="tab" data-tab="code-explanation">Code Explanation</div>
|
| 172 |
-
</div>
|
| 173 |
-
|
| 174 |
-
<!-- Tab Content -->
|
| 175 |
-
<div class="tab-content">
|
| 176 |
-
<!-- Include all tab panes from the HTML -->
|
| 177 |
-
<!-- ... (Code Analysis, GitHub Results, etc) ... -->
|
| 178 |
-
</div>
|
| 179 |
-
|
| 180 |
-
<!-- Chat Section -->
|
| 181 |
-
<div class="chat-section">
|
| 182 |
-
<!-- Chat interface from HTML -->
|
| 183 |
-
</div>
|
| 184 |
-
</div>
|
| 185 |
-
</div>
|
| 186 |
-
</div>
|
| 187 |
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
</html>
|
| 197 |
"""
|
| 198 |
|
| 199 |
# Helper functions for code analysis
|
|
@@ -665,40 +647,181 @@ def process_code_file(file_obj):
|
|
| 665 |
except Exception as e:
|
| 666 |
return None, f"Error processing file: {str(e)}", {}
|
| 667 |
|
| 668 |
-
# Gradio
|
| 669 |
-
with gr.Blocks(css=
|
| 670 |
current_session_id = gr.State(None)
|
| 671 |
code_state = gr.State({})
|
| 672 |
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
|
|
|
|
|
|
|
|
|
| 676 |
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 703 |
if __name__ == "__main__":
|
| 704 |
demo.launch()
|
|
|
|
| 49 |
# Dictionary to store user-specific vectorstores
|
| 50 |
user_vectorstores = {}
|
| 51 |
|
| 52 |
+
# Custom CSS for Tech theme
|
| 53 |
+
custom_css = """
|
| 54 |
+
:root {
|
| 55 |
+
--primary: #2563eb; /* Modern blue */
|
| 56 |
+
--secondary: #4f46e5; /* Indigo */
|
| 57 |
+
--accent: #dc2626; /* Red for accents */
|
| 58 |
+
--background: #f8fafc; /* Light slate */
|
| 59 |
+
--surface: #ffffff; /* White surfaces */
|
| 60 |
+
--text: #1e293b; /* Slate-800 */
|
| 61 |
+
--border: #e2e8f0; /* Slate-200 */
|
| 62 |
+
}
|
| 63 |
+
body {
|
| 64 |
+
background: var(--background);
|
| 65 |
+
font-family: 'Inter', sans-serif;
|
| 66 |
+
}
|
| 67 |
+
.container {
|
| 68 |
+
max-width: 1200px !important;
|
| 69 |
+
margin: 0 auto !important;
|
| 70 |
+
padding: 10px;
|
| 71 |
+
}
|
| 72 |
+
.header {
|
| 73 |
+
background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
|
| 74 |
+
color: white;
|
| 75 |
+
padding: 1.5rem;
|
| 76 |
+
border-radius: 0;
|
| 77 |
+
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
|
| 78 |
+
}
|
| 79 |
+
.header-title {
|
| 80 |
+
font-size: 1.875rem;
|
| 81 |
+
font-weight: 700;
|
| 82 |
+
letter-spacing: -0.025em;
|
| 83 |
+
}
|
| 84 |
+
.header-subtitle {
|
| 85 |
+
color: var(--dark-text);
|
| 86 |
+
font-size: 1rem;
|
| 87 |
+
text-align: center;
|
| 88 |
+
margin-top: 5px;
|
| 89 |
+
}
|
| 90 |
+
.chat-container {
|
| 91 |
+
background: var(--surface);
|
| 92 |
+
border-radius: 0.75rem;
|
| 93 |
+
border: 1px solid var(--border);
|
| 94 |
+
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
|
| 95 |
+
}
|
| 96 |
+
.tool-container {
|
| 97 |
+
background-color: var(--white);
|
| 98 |
+
border-radius: 12px;
|
| 99 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
| 100 |
+
padding: 15px;
|
| 101 |
+
margin-bottom: 20px;
|
| 102 |
+
}
|
| 103 |
+
.code-block {
|
| 104 |
+
background-color: var(--code-bg);
|
| 105 |
+
padding: 12px;
|
| 106 |
+
border-radius: 8px;
|
| 107 |
+
font-family: 'Roboto Mono', monospace;
|
| 108 |
+
overflow-x: auto;
|
| 109 |
+
margin: 10px 0;
|
| 110 |
+
border-left: 3px solid var(--primary-color);
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
/* Modern input styling */
|
| 114 |
+
.input-box {
|
| 115 |
+
background: var(--surface);
|
| 116 |
+
border: 2px solid var(--border);
|
| 117 |
+
border-radius: 0.5rem;
|
| 118 |
+
padding: 0.75rem 1rem;
|
| 119 |
+
transition: all 0.2s;
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
.input-box:focus {
|
| 123 |
+
border-color: var(--primary);
|
| 124 |
+
box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
/* Button styles */
|
| 128 |
+
.send-btn, .clear-btn {
|
| 129 |
+
border-radius: 0.5rem;
|
| 130 |
+
padding: 0.75rem 1.5rem;
|
| 131 |
+
font-weight: 500;
|
| 132 |
+
transition: all 0.2s;
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
.send-btn {
|
| 136 |
+
background: var(--primary);
|
| 137 |
+
color: white;
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
.send-btn:hover {
|
| 141 |
+
background: #1d4ed8;
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
/* File upload section */
|
| 145 |
+
.upload-section {
|
| 146 |
+
border: 2px dashed var(--border);
|
| 147 |
+
border-radius: 0.75rem;
|
| 148 |
+
padding: 2rem;
|
| 149 |
+
text-align: center;
|
| 150 |
+
background: var(--surface);
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
/* Chat bubbles */
|
| 154 |
+
.message-user {
|
| 155 |
+
background: var(--primary);
|
| 156 |
+
color: white;
|
| 157 |
+
border-radius: 1rem 1rem 0.25rem 1rem;
|
| 158 |
+
max-width: 80%;
|
| 159 |
+
margin-left: auto;
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
.message-bot {
|
| 163 |
+
background: var(--surface);
|
| 164 |
+
color: var(--text);
|
| 165 |
+
border: 1px solid var(--border);
|
| 166 |
+
border-radius: 1rem 1rem 1rem 0.25rem;
|
| 167 |
+
max-width: 80%;
|
| 168 |
+
margin-right: auto;
|
| 169 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
+
/* Metrics display */
|
| 172 |
+
.stats-box {
|
| 173 |
+
background: var(--surface);
|
| 174 |
+
border: 1px solid var(--border);
|
| 175 |
+
border-radius: 0.75rem;
|
| 176 |
+
padding: 1.5rem;
|
| 177 |
+
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
|
| 178 |
+
}
|
|
|
|
| 179 |
"""
|
| 180 |
|
| 181 |
# Helper functions for code analysis
|
|
|
|
| 647 |
except Exception as e:
|
| 648 |
return None, f"Error processing file: {str(e)}", {}
|
| 649 |
|
| 650 |
+
# Update the Gradio interface
|
| 651 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 652 |
current_session_id = gr.State(None)
|
| 653 |
code_state = gr.State({})
|
| 654 |
|
| 655 |
+
gr.HTML("""
|
| 656 |
+
<div class="header">
|
| 657 |
+
<div class="header-title">Tech-Vision AI</div>
|
| 658 |
+
<div class="header-subtitle">Advanced Code Analysis & Technical Assistant</div>
|
| 659 |
+
</div>
|
| 660 |
+
""")
|
| 661 |
|
| 662 |
+
with gr.Row(elem_classes="container"):
|
| 663 |
+
with gr.Column(scale=1, min_width=300):
|
| 664 |
+
file_input = gr.File(
|
| 665 |
+
label="Upload Code File",
|
| 666 |
+
file_types=[".py", ".js", ".java", ".cpp", ".c", ".cs", ".php", ".rb", ".go", ".ts"],
|
| 667 |
+
type="binary"
|
| 668 |
+
)
|
| 669 |
+
upload_button = gr.Button("Analyze Code", variant="primary")
|
| 670 |
+
file_status = gr.Markdown("No file uploaded yet")
|
| 671 |
+
model_dropdown = gr.Dropdown(
|
| 672 |
+
choices=["llama3-70b-8192", "mixtral-8x7b-32768", "gemma-7b-it"],
|
| 673 |
+
value="llama3-70b-8192",
|
| 674 |
+
label="Select Model"
|
| 675 |
+
)
|
| 676 |
+
|
| 677 |
+
# Developer Tools Section
|
| 678 |
+
gr.Markdown("### Developer Tools", elem_classes="tool-title")
|
| 679 |
+
with gr.Group(elem_classes="tool-container"): # Replace Box with Group
|
| 680 |
+
with gr.Tabs():
|
| 681 |
+
with gr.TabItem("GitHub Search"):
|
| 682 |
+
repo_query = gr.Textbox(label="Search Query", placeholder="Enter keywords to search for repositories")
|
| 683 |
+
with gr.Row():
|
| 684 |
+
language = gr.Dropdown(
|
| 685 |
+
choices=["any", "JavaScript", "Python", "Java", "C++", "TypeScript", "Go", "Rust", "PHP", "C#"],
|
| 686 |
+
value="any",
|
| 687 |
+
label="Language"
|
| 688 |
+
)
|
| 689 |
+
min_stars = gr.Dropdown(
|
| 690 |
+
choices=["0", "10", "50", "100", "1000", "10000"],
|
| 691 |
+
value="0",
|
| 692 |
+
label="Min Stars"
|
| 693 |
+
)
|
| 694 |
+
sort_by = gr.Dropdown(
|
| 695 |
+
choices=["stars", "forks", "updated"],
|
| 696 |
+
value="stars",
|
| 697 |
+
label="Sort By"
|
| 698 |
+
)
|
| 699 |
+
repo_search_btn = gr.Button("Search Repositories")
|
| 700 |
+
|
| 701 |
+
with gr.TabItem("Stack Overflow"):
|
| 702 |
+
stack_query = gr.Textbox(label="Search Query", placeholder="Enter your technical question")
|
| 703 |
+
with gr.Row():
|
| 704 |
+
tag = gr.Dropdown(
|
| 705 |
+
choices=["any", "python", "javascript", "java", "c++", "react", "node.js", "android", "ios", "sql"],
|
| 706 |
+
value="any",
|
| 707 |
+
label="Tag"
|
| 708 |
+
)
|
| 709 |
+
so_sort_by = gr.Dropdown(
|
| 710 |
+
choices=["votes", "newest", "activity"],
|
| 711 |
+
value="votes",
|
| 712 |
+
label="Sort By"
|
| 713 |
+
)
|
| 714 |
+
so_search_btn = gr.Button("Search Stack Overflow")
|
| 715 |
+
|
| 716 |
+
with gr.TabItem("Code Explainer"):
|
| 717 |
+
code_input = gr.Textbox(
|
| 718 |
+
label="Code to Explain",
|
| 719 |
+
placeholder="Paste your code here...",
|
| 720 |
+
lines=10
|
| 721 |
+
)
|
| 722 |
+
explain_btn = gr.Button("Explain Code")
|
| 723 |
+
|
| 724 |
+
with gr.Column(scale=2, min_width=600):
|
| 725 |
+
with gr.Tabs():
|
| 726 |
+
with gr.TabItem("Code Analysis"):
|
| 727 |
+
with gr.Column(elem_classes="code-viewer-container"):
|
| 728 |
+
code_metrics = gr.Markdown("No code analyzed yet", elem_classes="stats-box")
|
| 729 |
+
code_recommendations = gr.Markdown("", elem_classes="recommendations-box")
|
| 730 |
+
|
| 731 |
+
with gr.TabItem("GitHub Results"):
|
| 732 |
+
repo_results = gr.Markdown("Search for repositories to see results here")
|
| 733 |
+
|
| 734 |
+
with gr.TabItem("Stack Overflow Results"):
|
| 735 |
+
stack_results = gr.Markdown("Search for questions to see results here")
|
| 736 |
+
|
| 737 |
+
with gr.TabItem("Code Explanation"):
|
| 738 |
+
code_explanation = gr.Markdown("Paste your code and click 'Explain Code' to see an explanation here")
|
| 739 |
|
| 740 |
+
with gr.Row(elem_classes="container"):
|
| 741 |
+
with gr.Column(scale=2, min_width=600):
|
| 742 |
+
chatbot = gr.Chatbot(
|
| 743 |
+
height=500,
|
| 744 |
+
show_copy_button=True,
|
| 745 |
+
elem_classes="chat-container",
|
| 746 |
+
type="messages"
|
| 747 |
+
)
|
| 748 |
+
with gr.Row():
|
| 749 |
+
msg = gr.Textbox(
|
| 750 |
+
show_label=False,
|
| 751 |
+
placeholder="Ask about your code, type /github to search repos, or /stack to search Stack Overflow...",
|
| 752 |
+
scale=5
|
| 753 |
+
)
|
| 754 |
+
send_btn = gr.Button("Send", scale=1)
|
| 755 |
+
clear_btn = gr.Button("Clear Conversation")
|
| 756 |
+
|
| 757 |
+
# Update event handlers
|
| 758 |
+
upload_button.click(
|
| 759 |
+
lambda x: process_code_file(x),
|
| 760 |
+
inputs=[file_input],
|
| 761 |
+
outputs=[current_session_id, file_status, code_state]
|
| 762 |
+
).then(
|
| 763 |
+
lambda state: (
|
| 764 |
+
f"### Code Analysis Results\n\n"
|
| 765 |
+
f"**Language:** {state.get('language', 'Unknown')}\n"
|
| 766 |
+
f"**Total Lines:** {state.get('total_lines', 0)}\n"
|
| 767 |
+
f"**Code Lines:** {state.get('code_lines', 0)}\n"
|
| 768 |
+
f"**Comment Lines:** {state.get('comments', 0)}\n"
|
| 769 |
+
f"**Functions:** {state.get('functions', 0)}\n"
|
| 770 |
+
f"**Classes:** {state.get('classes', 0)}\n"
|
| 771 |
+
f"**Complexity Score:** {state.get('cyclomatic_complexity', 0)}\n"
|
| 772 |
+
),
|
| 773 |
+
inputs=[code_state],
|
| 774 |
+
outputs=[code_metrics]
|
| 775 |
+
).then(
|
| 776 |
+
lambda state: generate_recommendations(state),
|
| 777 |
+
inputs=[code_state],
|
| 778 |
+
outputs=[code_recommendations]
|
| 779 |
+
)
|
| 780 |
+
|
| 781 |
+
msg.submit(
|
| 782 |
+
generate_response,
|
| 783 |
+
inputs=[msg, current_session_id, model_dropdown, chatbot],
|
| 784 |
+
outputs=[chatbot]
|
| 785 |
+
).then(lambda: "", None, [msg])
|
| 786 |
+
|
| 787 |
+
send_btn.click(
|
| 788 |
+
generate_response,
|
| 789 |
+
inputs=[msg, current_session_id, model_dropdown, chatbot],
|
| 790 |
+
outputs=[chatbot]
|
| 791 |
+
).then(lambda: "", None, [msg])
|
| 792 |
|
| 793 |
+
clear_btn.click(
|
| 794 |
+
lambda: ([], None, "No file uploaded", {}, None),
|
| 795 |
+
None,
|
| 796 |
+
[chatbot, current_session_id, file_status, code_state, code_metrics]
|
| 797 |
+
)
|
| 798 |
+
|
| 799 |
+
# Tech tool handlers
|
| 800 |
+
repo_search_btn.click(
|
| 801 |
+
perform_repo_search,
|
| 802 |
+
inputs=[repo_query, language, sort_by, min_stars],
|
| 803 |
+
outputs=[repo_results]
|
| 804 |
+
)
|
| 805 |
|
| 806 |
+
so_search_btn.click(
|
| 807 |
+
perform_stack_search,
|
| 808 |
+
inputs=[stack_query, tag, so_sort_by],
|
| 809 |
+
outputs=[stack_results]
|
| 810 |
+
)
|
| 811 |
+
|
| 812 |
+
explain_btn.click(
|
| 813 |
+
explain_code,
|
| 814 |
+
inputs=[code_input],
|
| 815 |
+
outputs=[code_explanation]
|
| 816 |
+
)
|
| 817 |
+
|
| 818 |
+
# Add footer with attribution
|
| 819 |
+
gr.HTML("""
|
| 820 |
+
<div style="text-align: center; margin-top: 20px; padding: 10px; color: #666; font-size: 0.8rem; border-top: 1px solid #eee;">
|
| 821 |
+
Created by Calvin Allen Crawford
|
| 822 |
+
</div>
|
| 823 |
+
""")
|
| 824 |
+
|
| 825 |
+
# Launch the app
|
| 826 |
if __name__ == "__main__":
|
| 827 |
demo.launch()
|