YashJD's picture
Changes 1
4eba3d2
raw
history blame
3.41 kB
import streamlit as st
import pandas as pd
import PyPDF2
import io
import os
from dotenv import load_dotenv
import requests
import time
# Load environment variables
load_dotenv()
PERPLEXITY_API_KEY = os.getenv("PERPLEXITY_API_KEY")
PERPLEXITY_API_URL = "https://api.perplexity.ai/chat/completions"
def call_perplexity_api(prompt: str) -> str:
"""Call Perplexity AI with a prompt, return the text response if successful."""
headers = {
"Authorization": f"Bearer {PERPLEXITY_API_KEY}",
"Content-Type": "application/json",
}
payload = {
"model": "llama-3.1-sonar-small-128k-chat",
"messages": [{"role": "user", "content": prompt}],
"temperature": 0.3,
}
try:
response = requests.post(PERPLEXITY_API_URL, headers=headers, json=payload)
response.raise_for_status()
return response.json()["choices"][0]["message"]["content"]
except Exception as e:
st.error(f"API Error: {str(e)}")
return ""
def main():
st.title("Research Corpus Synthesis Tool")
# File uploader
uploaded_file = st.file_uploader("Upload CSV file", type="csv")
if uploaded_file:
if st.button("Process CSV"):
# Initialize progress bar
progress_bar = st.progress(0)
status_text = st.empty()
# Read CSV file into DataFrame
df = pd.read_csv(uploaded_file)
# Initialize results dictionary
results = []
# Process each column
for i, column in enumerate(df.columns):
status_text.text(f"Processing column: {column}")
# Extract text from column
text = " ".join(df[column].astype(str).tolist())
# Generate prompt
prompt = f"You are a Professional Researcher and Analyser with 10 yrs of Experience.Find details and Elaborate on Top Trends,Theories,Methods,FrameWorks with this topic ({column}):\n\n{text[:5000]}" # Limit text to avoid token limits
# Call Perplexity API
result = call_perplexity_api(prompt)
results.append({"Column": column, "Result": result})
# Update progress
progress = (i + 1) / len(df.columns)
progress_bar.progress(progress)
# Create DataFrame from results
results_df = pd.DataFrame(results)
# Convert DataFrame to CSV
csv = results_df.to_csv(index=False)
# Create download button
st.download_button(
label="Download Results as CSV",
data=csv,
file_name="column_trends_analysis.csv",
mime="text/csv",
)
st.subheader("Analysis Results")
styled_df = results_df.style.set_properties(**{
'background-color': '#f9f9f9',
'color': '#333',
'border-color': 'black',
'border-width': '1px',
'border-style': 'solid',
'font-family': 'Arial, sans-serif',
'font-size': '14px',
'text-align': 'left',
'padding': '10px'
})
st.dataframe(styled_df)
status_text.text("Processing complete!")
progress_bar.progress(1.0)
if __name__ == "__main__":
main()