# 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() |