File size: 4,177 Bytes
b91146d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
106
107
108
109
110
111
import streamlit as st
import pandas as pd
import requests
import json
import os
from dotenv import load_dotenv

# 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 generate_research_paper(df: pd.DataFrame, topic: str) -> dict:
    """

    For each column in the DataFrame, generate a research paper section (200-500 words)

    that addresses the data in that column on the given topic. Return a dict: column -> text.

    """
    paper_sections = {}
    for col in df.columns:
        # Convert all non-null rows in the column to strings and join them for context
        col_values = df[col].dropna().astype(str).tolist()
        # We'll truncate if there's a ton of text
        sample_text = " | ".join(col_values[:50])  # limit to first 50 rows for brevity

        prompt = f"""

        Topic: {topic}

        Column: {col}

        Data Samples: {sample_text}



        Generate a well-structured research paper section that addresses the topic above,

        referencing relevant information from the column data.

        The section should be at least 100 words and at most 150 words.

        Provide insights, examples, and possible research directions integrating the corpus data.

        """
        section_text = call_perplexity_api(prompt)
        paper_sections[col] = section_text.strip() if section_text else ""
    return paper_sections


def format_paper(paper_dict: dict, topic: str) -> str:
    """

    Format the generated paper into a Markdown string.

    Add the topic as the main title, each column name as a heading, and

    the corresponding text as paragraph content.

    """
    md_text = f"# Research Paper on: {topic}\n\n"
    for col, content in paper_dict.items():
        md_text += f"## {col}\n{content}\n\n"
    return md_text


def main():
    st.title("Topic + Corpus-Based Research Paper Generator")

    topic_input = st.text_input("Enter the topic for the research paper:")
    uploaded_file = st.file_uploader("Upload CSV corpus file", type="csv")

    if uploaded_file:
        df = pd.read_csv(uploaded_file)
        st.write("### Preview of Uploaded Data")
        st.dataframe(df.head())

        if st.button("Generate Research Paper"):
            if topic_input.strip():
                st.info("Generating paper based on the topic and the corpus columns...")
                with st.spinner("Calling Perplexity AI..."):
                    paper = generate_research_paper(df, topic_input)
                    if paper:
                        formatted_paper = format_paper(paper, topic_input)
                        st.success("Research Paper Generated Successfully!")
                        st.write(formatted_paper)

                        st.download_button(
                            label="Download Paper as Markdown",
                            data=formatted_paper,
                            file_name="research_paper.md",
                            mime="text/markdown",
                        )
                    else:
                        st.error(
                            "Paper generation failed. Please check Perplexity API key."
                        )
            else:
                st.warning("Please enter a valid topic.")


if __name__ == "__main__":
    main()