|
import streamlit as st |
|
import pandas as pd |
|
import requests |
|
import json |
|
import os |
|
from dotenv import load_dotenv |
|
|
|
|
|
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: |
|
|
|
col_values = df[col].dropna().astype(str).tolist() |
|
|
|
sample_text = " | ".join(col_values[:50]) |
|
|
|
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() |
|
|