import os import streamlit as st import pandas as pd import numpy as np import plotly.express as px import plotly.graph_objects as go from openai import OpenAI import requests from dotenv import load_dotenv import time import io import base64 # from streamlit_option_menu import option_menu st.markdown('', unsafe_allow_html=True) st.markdown(""" """, unsafe_allow_html=True) st.set_page_config( page_title="BioVix", layout="wide", initial_sidebar_state="expanded" ) st.markdown(""" """, unsafe_allow_html=True) st.markdown(""" """, unsafe_allow_html=True) load_dotenv() deepseek_api_key = os.getenv("DEEPSEEK_API_KEY") gpt_api_key = os.getenv("GPT_API_KEY") qwen_api_key = os.getenv("QWEN_API_KEY") SEMANTIC_SCHOLAR_API_KEY = os.getenv("SEMANTIC_SCHOLAR_API_KEY") api_errors = [] if not deepseek_api_key: api_errors.append("DeepSeek API Key") if not gpt_api_key: api_errors.append("GPT API Key") if not qwen_api_key: api_errors.append("QWEN API Key") if not SEMANTIC_SCHOLAR_API_KEY: api_errors.append("Semantic Scholar API Key") if api_errors: st.markdown(f"""

Configuration Required

Missing API keys: {', '.join(api_errors)}

Please configure your .env file with the required API keys to continue.

""", unsafe_allow_html=True) st.stop() deepseek_client = OpenAI( base_url="https://openrouter.ai/api/v1", api_key=deepseek_api_key ) gpt_client = OpenAI( base_url="https://openrouter.ai/api/v1", api_key=gpt_api_key ) qwen_client = OpenAI( base_url="https://openrouter.ai/api/v1", api_key=qwen_api_key ) def clean_code(text: str) -> str: """Remove markdown fences or extra text from code output.""" return text.strip().replace("```python", "").replace("```", "") def generate_chart_code(columns: str, query: str) -> str: messages = [ {"role": "system", "content": "You are an intelligent data visualization assistant."}, {"role": "user", "content": f"The user uploaded a dataset with the following columns:\n{columns}\n\nThe user asked: '{query}'\n\nWrite valid Python code that:\n1. Uses Plotly (px or go) to create the most appropriate chart.\n2. The dataframe is available as `df`.\n3. Save the chart as a variable named `fig`.\n\nOnly return the code. Do not include explanations."} ] try: response = deepseek_client.chat.completions.create( model="deepseek/deepseek-chat-v3.1", messages=messages, temperature=0, ) return clean_code(response.choices[0].message.content) except Exception as e: st.error(f" Error generating chart code: {e}") return "" def fix_chart_code(code: str, error: str) -> str: messages = [ {"role": "system", "content": ( "You are a data visualization assistant. " "IMPORTANT RULES:\n" "1. Never create or inject fake/sample dataframes (like df = pd.DataFrame(...)).\n" "2. Always assume the user already uploaded a dataframe named `df`.\n" "3. If certain required columns are missing, insert a validation check that raises a ValueError with a clear message.\n" "4. Always return corrected Python code that produces a Plotly chart named `fig`.\n" "5. Only return the corrected code, no explanations." )}, {"role": "user", "content": f"The following code failed:\n\n{code}\n\nError:\n{error}\n\nFix the code according to the rules."} ] try: response = deepseek_client.chat.completions.create( model="deepseek/deepseek-chat-v3.1", messages=messages, temperature=0, ) return clean_code(response.choices[0].message.content) except Exception as e: st.error(f"Error fixing chart code: {e}") return code def generate_explanation(query: str, columns: str, stats_summary: str) -> str: code = generate_chart_code(columns, query) messages = [ {"role": "system", "content": "You are a data visualization expert. Provide clear, concise explanations of generated chart.Don't show the <|begin▁of▁sentence|>"}, {"role": "user", "content": f"User query: '{query}'\nGenerated Graph code: '{code}'\nDataset columns: {columns}\nKey statistics from the chart data: {stats_summary}\n\nExplain the chart in natural language: Describe what it shows, key trends, insights, and implications. Keep it to 3-5 sentences. Be engaging and insightful."} ] try: response = deepseek_client.chat.completions.create( model="deepseek/deepseek-chat-v3.1", messages=messages, temperature=0.3, ) return response.choices[0].message.content.strip() except Exception as e: st.error(f"Error generating explanation: {e}") return "Could not generate explanation due to API error." def analyze_explanation_for_query(explanation: str, original_query: str, columns: str) -> str: messages = [ { "role": "system", "content": ( "You are an academic and literature research assistant." ), }, { "role": "user", "content": ( f"Dataset columns: {columns}\n" f"Chart explanation: {explanation}\n" f"Original user query: {original_query}\n\n" "Craft precise, a domain-specific query (5–12 words) for Semantic Scholar " "based on the dataset columns, chart explanation, and user query. " "Generate a concise academic search query specific to this dataset." "If dataset columns contain abbreviations or symbols (e.g., APPL), expand them into their real-world entity names (e.g., Apple). " "Queries must incorporate relevant dataset entities (company names, regions, populations, etc.) " "Focus on methods, variables, and domain terminology of dataset. " "Target literature that can support or challenge the observed trend. " "Do not include chart names or explanations. " "your response should be a concise academic search query only, no reasoning and explanation! ." ), }, ] try: response = deepseek_client.chat.completions.create( model="deepseek/deepseek-chat-v3.1", messages=messages, temperature=0.1, ) return response.choices[0].message.content.strip() except Exception as e: st.error(f"Error generating academic query: {e}") return original_query def search_semantic_scholar(search_query: str, retries: int = 3, backoff: int = 5): """Search Semantic Scholar API for the most relevant papers with retries & exponential backoff.""" url = "https://api.semanticscholar.org/graph/v1/paper/search" params = { "query": search_query, "limit": 5, "sort": "relevance", "fields": "title,authors,year,abstract,citationCount,url" } headers = {"x-api-key": SEMANTIC_SCHOLAR_API_KEY} for attempt in range(retries): try: response = requests.get(url, params=params, headers=headers, timeout=10) if response.status_code == 429: wait_time = backoff * (2 ** attempt) st.warning(f"Semantic Scholar rate limit hit (429). Retrying in {wait_time} seconds...") time.sleep(wait_time) continue response.raise_for_status() data = response.json() if "data" in data and data["data"]: papers = [] for paper in data["data"]: papers.append({ "Title": paper.get("title", "N/A"), "Authors": ", ".join([a.get("name", "N/A") for a in paper.get("authors", [])]), "Year": paper.get("year", "N/A"), "Citations": paper.get("citationCount", 0), "Abstract Snippet": paper.get("abstract", "N/A")[:200] + "..." if paper.get("abstract") else "N/A", "URL": paper.get("url", "N/A") }) return pd.DataFrame(papers) else: return pd.DataFrame({"Message": ["No relevant papers found."]}) except Exception as e: st.error(f"Semantic Scholar API error: {e}") if attempt == retries - 1: return pd.DataFrame({"Message": ["Search failed."]}) time.sleep(backoff) return pd.DataFrame({"Message": ["Failed after retries due to rate limits."]}) def interpret_graph_with_qwen(image_base64: str, question: str) -> str: """Interpret a graph image""" try: messages = [ { "role": "user", "content": [ { "type": "text", "text": f"Please analyze this graph/chart image and answer the following question: {question}\n\nProvide a concise and precise interpretation including trends, patterns, key insights, and any notable observations. If you are not clear about the graph/chart, say so and ask the user to provide more information.Answers should be no more than 6 to 7 lines." }, { "type": "image_url", "image_url": { "url": f"data:image/jpeg;base64,{image_base64}" } } ] } ] response = qwen_client.chat.completions.create( model="qwen/qwen3-vl-32b-instruct", messages=messages, max_tokens=1000, temperature=0.3 ) return response.choices[0].message.content.strip() except Exception as e: return f"Error interpreting graph with qwen: {str(e)}" def create_sample_data_from_drive(sample_choice: str) -> pd.DataFrame: """Load dataset from Google Drive for given sample_choice.""" try: if sample_choice == "Population Data": # Use the share link you provided share_url = "https://drive.google.com/file/d/1NttDmesL-sInl27PvYlFZRCUZNgpJQL8/view?usp=sharing" file_id = share_url.split("/")[-2] download_url = f"https://drive.google.com/uc?export=download&id={file_id}" df = pd.read_csv(download_url) return df if sample_choice == "Immigration Data": # Use the share link you provided share_url = "https://drive.google.com/file/d/1DB9107DwAphw9cfTnkO6LxqbHH6SgGzy/view?usp=drive_link" file_id = share_url.split("/")[-2] download_url = f"https://drive.google.com/uc?export=download&id={file_id}" df = pd.read_csv(download_url) return df except Exception as e: st.error(f"⚠️ Error creating sample data: {e}") return None if 'df' not in st.session_state: st.session_state.df = None if 'fig' not in st.session_state: st.session_state.fig = None # Show intro only if no dataset is loaded if st.session_state.df is None: st.markdown("""

What is BioVix?

BioVix is an innovative web-based tool that integrates data visualization, AI-powered interpretation, and relevant literature search—all from a single query. It allows users to visualize their uploaded data and discover pertinent research articles seamlessly. Additionally, BioVix supports both conversational and uploaded graph interpretation to enhance users’ understanding of their data trends and patterns.

""", unsafe_allow_html=True) st.header("Overview") with st.container(): # Centering using columns col1, col2, col3 = st.columns([1, 2, 1]) with col2: st.image("overview.jpg", width=900) with st.sidebar: st.markdown("""

Control Panel

""", unsafe_allow_html=True) st.markdown("### Upload Your File") uploaded_file = st.file_uploader( "Choose a data file", type=["csv", "tsv", "xlsx"], help="Supported formats: CSV, TSV, Excel (.xlsx)" ) st.markdown("### Sample Datasets") sample_choice = st.radio( "Or load a sample dataset:", ( "None","Population Data", "Immigration Data"), ) st.markdown("### AI Assistant") with st.expander("Data Q&A", expanded=False): qa_query = st.text_area( "Your question:", placeholder="e.g., What patterns do you see in this dataset?", height=80 ) if st.button("Ask AI", use_container_width=True): if not qa_query.strip(): st.warning("Please enter a question first.") elif st.session_state.df is None: st.error("No data loaded yet.") else: with st.spinner("AI is analyzing your data..."): try: df = st.session_state.df # Prepare data summary data_summary = f""" Dataset Info: - Shape: {df.shape[0]} rows, {df.shape[1]} columns - Columns: {', '.join(df.columns.tolist())} Data Preview (first 5 rows): {df.head().to_string()} Statistical Summary: {df.describe().to_string() if len(df.select_dtypes(include=['number']).columns) > 0 else 'No numeric columns'} Missing Values: {df.isnull().sum().to_dict()} """ messages = [ { "role": "system", "content": "You are a data analysis expert. Analyze the provided dataset and answer the user's question with clear, insightful responses. Focus on patterns, trends, and actionable insights." }, { "role": "user", "content": f"Your Answer should not be more than 6 to 7 lines. Here is my dataset:\n\n{data_summary}\n\nQuestion: {qa_query}\n\nProvide a well-structured but concise and precise answer based on the data.Summarize key findings and insights.don't use the *" } ] response = gpt_client.chat.completions.create( model="openai/gpt-oss-20b", messages=messages, temperature=0.7, max_tokens=800 ) answer = response.choices[0].message.content.strip() st.markdown("**AI Analysis:**") st.markdown(f"""
{answer}
""", unsafe_allow_html=True) except Exception as e: st.error(f"Error getting AI response: {str(e)}") with st.expander("Graph Interpreter", expanded=False): uploaded_graph = st.file_uploader( "Upload Graph Image", type=["png", "jpg", "jpeg"], key="graph_upload" ) if uploaded_graph: st.image(uploaded_graph, caption="Uploaded Graph", use_container_width=True) graph_question = st.text_area( "Question about the graph:", placeholder="e.g., What trends does this graph show?", key="graph_question", height=80 ) if st.button("Analyze Graph", use_container_width=True): if uploaded_graph is None: st.warning("Please upload a graph image first.") elif not graph_question.strip(): st.warning("Please enter a question first.") else: with st.spinner("Qwen is interpreting your graph..."): try: img_bytes = uploaded_graph.read() img_b64 = base64.b64encode(img_bytes).decode("utf-8") interpretation = interpret_graph_with_qwen(img_b64, graph_question) st.markdown("**Qwen's Analysis:**") st.markdown(f"""
{interpretation}
""", unsafe_allow_html=True) except Exception as e: st.error(f"Error interpreting graph: {e}") current_df = None if uploaded_file is not None: try: if uploaded_file.name.endswith(".csv"): current_df = pd.read_csv(uploaded_file) elif uploaded_file.name.endswith(".tsv"): current_df = pd.read_csv(uploaded_file, sep="\t") else: current_df = pd.read_excel(uploaded_file) current_df.columns = current_df.columns.str.strip() current_df.columns = current_df.columns.str.replace(r"\s+", "_", regex=True) current_df.columns = current_df.columns.str.replace(r"[^\w]", "", regex=True) st.session_state.df = current_df except Exception as e: st.markdown(f"""

File Processing Error

Unable to read the uploaded file: {str(e)}

Please ensure your file is in the correct format (CSV, TSV, or Excel).

""", unsafe_allow_html=True) st.session_state.df = None elif sample_choice != "None" and uploaded_file is None: current_df = create_sample_data_from_drive(sample_choice) if current_df is not None: st.session_state.df = current_df st.sidebar.success(f"Loaded {sample_choice} dataset.") else: st.sidebar.error("Failed to create sample dataset.") if st.session_state.df is not None: df = st.session_state.df rows, columns = df.shape col1, col2, col3, col4 = st.columns(4) with col1: st.markdown(f"""

{rows:,}

Rows

""", unsafe_allow_html=True) with col2: st.markdown(f"""

{columns}

Columns

""", unsafe_allow_html=True) with col3: numeric_cols = len(df.select_dtypes(include=['number']).columns) st.markdown(f"""

{numeric_cols}

Numeric

""", unsafe_allow_html=True) with col4: text_cols = len(df.select_dtypes(include=['object']).columns) st.markdown(f"""

{text_cols}

Text

""", unsafe_allow_html=True) st.markdown("""

Data Preview

""", unsafe_allow_html=True) st.dataframe( df.head(20), use_container_width=True, height=400 ) st.markdown("""

Chart Generation Studio

""", unsafe_allow_html=True) query = st.text_input( "Enter your Query", placeholder="e.g., 'Create a line chart showing sales trends over time with different colors for each product category'", help="Be specific about the type of chart, columns to use, and any styling preferences" ) # Add Submit button if st.button("Submit Query", use_container_width=True): if not query.strip(): st.warning("Please enter a query first.") elif st.session_state.df is None: st.error("❌ No dataset loaded yet.") else: df = st.session_state.df columns = ", ".join(df.columns) with st.spinner("Generating Graph..."): code = generate_chart_code(columns, query) if code: with st.expander("View Generated Code", expanded=False): st.code(code, language="python") max_attempts = 3 attempt = 0 success = False local_vars = {"df": df, "px": px, "go": go} while attempt < max_attempts and not success: try: with st.spinner(f"Executing visualization (attempt {attempt + 1})..."): exec(code, local_vars, local_vars) if "fig" not in local_vars: raise ValueError("No `fig` variable was created in the code.") st.markdown("""

Interactive Visualization

""", unsafe_allow_html=True) local_vars["fig"].update_layout(height=600) st.plotly_chart( local_vars["fig"], use_container_width=True, config={"displayModeBar": True} ) st.session_state.fig = local_vars["fig"] success = True # AI Insights and Research Section col1, col2 = st.columns([1, 1]) with col1: st.markdown("""

🤖 AI Insights

""", unsafe_allow_html=True) with st.spinner("Generating insights..."): numeric_cols = df.select_dtypes(include=['number']).columns stats_summary = f"Dataset shape: {df.shape}. Numeric columns: {list(numeric_cols)}. Sample stats: {df[numeric_cols].describe().to_dict() if len(numeric_cols) > 0 else 'No numeric data.'}" explanation = generate_explanation(query, columns, stats_summary) st.markdown(f"""
{explanation}
""", unsafe_allow_html=True) with col2: st.markdown("""

Academic Research

""", unsafe_allow_html=True) with st.spinner("Generating research query..."): academic_query = analyze_explanation_for_query(explanation, query, columns) st.markdown(f"""

Generated Research Query

{academic_query}

""", unsafe_allow_html=True) # Semantic Scholar Search st.markdown("""

📖 Relevant Research Papers (Semantic Scholar)

""", unsafe_allow_html=True) with st.spinner("🔍 Searching Semantic Scholar..."): papers_df = search_semantic_scholar(academic_query) if not papers_df.empty and "Title" in papers_df.columns: st.markdown("### Semantic Scholar Papers") for idx, row in papers_df.iterrows(): st.markdown(f"""

📎 {row['Title']}

Authors: {row['Authors']} | Year: {row['Year']} | Citations: {row['Citations']}

{row['Abstract Snippet']}

""", unsafe_allow_html=True) else: st.markdown("""

📭 No Semantic Scholar Papers Found

No relevant research papers were found for this query.

""", unsafe_allow_html=True) except Exception as e: attempt += 1 st.markdown(f"""

⚠️ Attempt {attempt} Failed

Error: {str(e)}

""", unsafe_allow_html=True) if attempt < max_attempts: with st.spinner("Auto-fixing code..."): current_hash = hash(code) new_code = fix_chart_code(code, str(e)) if hash(new_code) == current_hash: st.markdown("""

❌ Unable to Auto-Fix

The code correction system returned the same code. Please try rephrasing your query.

""", unsafe_allow_html=True) break code = new_code st.markdown(f"""

🔧 Auto-Corrected Code (Attempt {attempt})

""", unsafe_allow_html=True) with st.expander("View Corrected Code", expanded=False): st.code(code, language="python") local_vars = {"df": df, "px": px, "go": go} else: st.markdown("""

❌ Visualization Failed

Unable to generate a valid chart after multiple attempts. Please try:

""", unsafe_allow_html=True) else: st.markdown("""

❌ Code Generation Failed

Unable to generate chart code. Please check your API configuration or try a simpler query.

""", unsafe_allow_html=True)