File size: 3,406 Bytes
e107ee4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4eba3d2
e107ee4
 
4eba3d2
 
e107ee4
4eba3d2
 
 
 
e107ee4
 
 
4eba3d2
 
 
 
e107ee4
 
4eba3d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e107ee4
4eba3d2
 
e107ee4
 
 
 
4eba3d2
 
e107ee4
4eba3d2
e107ee4
4eba3d2
 
 
 
 
 
 
 
 
 
 
 
e107ee4
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
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()