File size: 14,270 Bytes
4eba3d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e107ee4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4eba3d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e107ee4
 
 
 
 
 
 
 
 
4eba3d2
e107ee4
 
4eba3d2
 
 
 
 
e107ee4
 
 
 
 
 
 
 
 
 
 
 
4eba3d2
e107ee4
 
4eba3d2
 
e107ee4
4eba3d2
 
 
e107ee4
4eba3d2
e107ee4
 
 
4eba3d2
e107ee4
 
4eba3d2
 
 
 
 
 
 
 
e107ee4
 
 
 
 
 
 
 
 
4eba3d2
e107ee4
4eba3d2
e107ee4
 
 
 
 
 
 
 
 
 
 
 
 
 
4eba3d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e107ee4
 
4eba3d2
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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# 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) -> dict:
#     """
#     For each column in the DataFrame, generate a research paper section (200-500 words)
#     that addresses the data in that column. Return a dict mapping 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 this is huge
#         sample_text = " | ".join(col_values[:50])  # limit to first 50 rows for brevity
#         prompt = f"""
#         Topic: {col}
#         Data Sample: {sample_text}

#         Generate a professional research paper section for the above column.
#         The section should be at least 100 words and at most 150 words,
#         focusing on key insights, challenges, and potential research angles.
#         Integrate the data samples as context for the content.
#         """
#         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) -> str:
#     """
#     Format the generated paper into a Markdown string.
#     Each column name is used as a heading, and the text is placed under it.
#     """
#     md_text = "# Generated Research Paper\n\n"
#     for col, content in paper_dict.items():
#         md_text += f"## {col}\n{content}\n\n"
#     return md_text


# def main():
#     st.title("Corpus-based Research Paper Generator")

#     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"):
#             st.info("Generating paper based on the columns of your corpus...")
#             with st.spinner("Calling Perplexity AI..."):
#                 paper = generate_research_paper(df)
#                 if paper:
#                     formatted_paper = format_paper(paper)
#                     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."
#                     )


# if __name__ == "__main__":
#     main()
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) -> dict:
#     """
#     For each column in the DataFrame, generate a research paper section (200-500 words)
#     that addresses the data in that column. Return a dict mapping 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 this is huge
#         sample_text = " | ".join(col_values[:50])  # limit to first 50 rows for brevity
#         prompt = f"""
#         Topic: {col}
#         Data Sample: {sample_text}

#         Generate a professional research paper section for the above column.
#         The section should be at least 100 words and at most 150 words,
#         focusing on key insights, challenges, and potential research angles.
#         Integrate the data samples as context for the content.
#         """
#         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) -> str:
#     """
#     Format the generated paper into a Markdown string.
#     Each column name is used as a heading, and the text is placed under it.
#     """
#     md_text = "# Generated Research Paper\n\n"
#     for col, content in paper_dict.items():
#         md_text += f"## {col}\n{content}\n\n"
#     return md_text


# def main():
#     st.title("Corpus-based Research Paper Generator")

#     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"):
#             st.info("Generating paper based on the columns of your corpus...")
#             with st.spinner("Calling Perplexity AI..."):
#                 paper = generate_research_paper(df)
#                 if paper:
#                     formatted_paper = format_paper(paper)
#                     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."
#                     )


# if __name__ == "__main__":
#     main()
#def generate_research_paper(df: pd.DataFrame, gaps_analysis: str, topic: str, journal: str, format: str) -> dict:
    """
    For each column in the DataFrame, generate a research paper section (200-500 words)
    that addresses the data in that column. Return a dict mapping 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 this is huge
        print(col)
        sample_text = " | ".join(col_values[:50])  # limit to first 50 rows for brevity
        prompt = f"""
        Topic: {topic}
        Journal/Conference: {journal}
        Format: {format}
        Gaps Analysis: {gaps_analysis}
        Column: {col}
        Data Sample: {sample_text}

        Generate a professional research paper section for the above column.
        The section should be at least 100 words and at most 150 words,
        focusing on key insights, challenges, and potential research angles.
        Integrate the data samples as context for the content.
        """
        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, journal: str, format: str) -> str:
    """
    Format the generated paper into a Markdown string.
    Add the topic, journal, and format 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"
    md_text += f"## Journal/Conference: {journal}\n\n"
    md_text += f"## Format: {format}\n\n"
    for col, content in paper_dict.items():
        md_text += f"### {col}\n{content}\n\n"
    return md_text


#def main():
    st.title("Corpus-based Research Paper Generator")

    topic_input = st.text_input("Enter the topic for the research paper:")
    journal_input = st.text_input("Enter the Journal/Conference aimed to publish:")
    format_input = st.text_input("Enter the format of the research paper:")
    gaps_analysis_file = st.file_uploader("Upload Gaps Analysis (.txt file)", type="txt")
    gaps_analysis = ""
    if gaps_analysis_file:
        gaps_analysis = gaps_analysis_file.getvalue().decode("utf-8")

    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"):
            st.info("Generating paper based on the columns of your corpus...")
            with st.spinner("Calling Perplexity AI..."):
                paper = generate_research_paper(df, gaps_analysis, topic_input, journal_input, format_input)
                if paper:
                    formatted_paper = format_paper(paper, topic_input, journal_input, format_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."
                    )

def generate_research_paper(df: pd.DataFrame, gaps_analysis: str, topic: str, journal: str, format: str) -> str:
    """
    Generate a research paper based on the entire DataFrame, the topic, journal, and format.
    """
    # Convert the entire DataFrame to a string
    df_string = df.to_string(index=False)

    # Create the prompt
    prompt = f"""
    Topic: {topic}
    Journal/Conference: {journal}
    Format: {format}
    Gaps Analysis: {gaps_analysis}
    Data:
    {df_string}

    Generate a professional research paper based on the above data.
    The paper should be well-structured, focusing on key insights, challenges, and potential research angles.
    Use the Gaps Analysis to identify areas for improvement and future work and fill the gaps in the new paper.
    Use the data as a reference to support your arguments, dont directly copy the data.
    Ensure the paper is formatted according to the specified journal/conference format.
    """

    # Call the Perplexity API
    paper_text = call_perplexity_api(prompt)
    return paper_text.strip() if paper_text else ""

def format_paper(paper_text: str, topic: str, journal: str, format: str) -> str:
    """
    Format the generated paper into a Markdown string.
    Add the topic, journal, and format as the main title, and the paper text as content.
    """
    md_text = f"# Research Paper on: {topic}\n\n"
    md_text += paper_text
    return md_text

def main():
    st.title("Corpus-based Research Paper Generator")

    topic_input = st.text_input("Enter the topic for the research paper:")
    journal_input = st.text_input("Enter the Journal/Conference aimed to publish:")
    format_input = st.text_input("Enter the format of the research paper:")
    gaps_analysis_file = st.file_uploader("Upload Gaps Analysis (.txt file)", type="txt")
    gaps_analysis = ""
    if gaps_analysis_file:
        gaps_analysis = gaps_analysis_file.getvalue().decode("utf-8")

    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"):
            st.info("Generating paper based on the columns of your corpus...")
            with st.spinner("Calling Perplexity AI..."):
                paper_text = generate_research_paper(df, gaps_analysis, topic_input, journal_input, format_input)
                if paper_text:
                    formatted_paper = format_paper(paper_text, topic_input, journal_input, format_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."
                    )

if __name__ == "__main__":
    main()