Ani14 commited on
Commit
2d473c3
Β·
verified Β·
1 Parent(s): 9e53385

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -0
app.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import arxiv
3
+ import requests
4
+ import streamlit as st
5
+ from dotenv import load_dotenv
6
+ from duckduckgo_search import DDGS
7
+ import subprocess
8
+
9
+ # Load API Key
10
+ load_dotenv()
11
+ API_KEY = os.getenv("OPENROUTER_API_KEY")
12
+
13
+ def call_llm(prompt):
14
+ url = "https://openrouter.ai/api/v1/chat/completions"
15
+ headers = {"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"}
16
+ data = {
17
+ "model": "google/gemma-3-1b-it:free",
18
+ "messages": [{"role": "user", "content": prompt}]
19
+ }
20
+ res = requests.post(url, headers=headers, json=data)
21
+ if res.status_code == 200:
22
+ return res.json()['choices'][0]['message']['content']
23
+ else:
24
+ return f"❌ Error: {res.status_code} - {res.text}"
25
+
26
+ def get_arxiv_papers(topic):
27
+ search = arxiv.Search(query=topic, max_results=5, sort_by=arxiv.SortCriterion.SubmittedDate)
28
+ papers = []
29
+ for result in search.results():
30
+ paper = {
31
+ "title": result.title,
32
+ "summary": result.summary,
33
+ "url": result.pdf_url
34
+ }
35
+ papers.append(paper)
36
+ return papers
37
+
38
+ def fetch_image_url(query):
39
+ with DDGS() as ddgs:
40
+ results = list(ddgs.images(query, max_results=1))
41
+ if results:
42
+ return results[0]['image']
43
+ return None
44
+
45
+ def create_latex(paper_title, full_content, image_url, file_name="research_paper.tex"):
46
+ latex = f"""
47
+ \\documentclass[12pt]{{article}}
48
+ \\usepackage{{graphicx}}
49
+ \\usepackage[margin=1in]{{geometry}}
50
+ \\usepackage{{hyperref}}
51
+ \\title{{\\textbf{{{paper_title}}}}}
52
+ \\date{{\\today}}
53
+ \\begin{{document}}
54
+ \\maketitle
55
+
56
+ \\begin{{figure}}[h]
57
+ \\centering
58
+ \\includegraphics[width=0.6\\textwidth]{{image.jpg}}
59
+ \\caption{{Relevant diagram for {paper_title}}}
60
+ \\end{{figure}}
61
+
62
+ {full_content.replace('\\n', '\\\\ \\n')}
63
+
64
+ \\end{{document}}
65
+ """
66
+ with open(file_name, "w") as f:
67
+ f.write(latex)
68
+
69
+ # Save image
70
+ if image_url:
71
+ img_data = requests.get(image_url).content
72
+ with open("image.jpg", "wb") as img:
73
+ img.write(img_data)
74
+
75
+ subprocess.run(["pdflatex", file_name], stdout=subprocess.DEVNULL)
76
+ return "research_paper.pdf"
77
+
78
+ # Streamlit UI
79
+ st.set_page_config("MTech AI Research Generator", layout="wide")
80
+ st.title("πŸ“š Deep Research Paper Generator (AI + arXiv + PDF Export)")
81
+
82
+ topic = st.text_input("πŸ” Enter your research topic:")
83
+
84
+ if topic:
85
+ with st.spinner("Fetching latest papers from arXiv..."):
86
+ papers = get_arxiv_papers(topic)
87
+ summaries = "\n\n".join([f"Title: {p['title']}\nSummary: {p['summary']}" for p in papers])
88
+ st.success("βœ… Papers Retrieved!")
89
+ st.subheader("πŸ“„ Top 5 arXiv Papers")
90
+ for p in papers:
91
+ st.markdown(f"**{p['title']}** \n[PDF Link]({p['url']}) \n> {p['summary'][:300]}...\n")
92
+
93
+ with st.spinner("Analyzing literature & generating novel paper..."):
94
+ gap = call_llm(f"Identify current research gaps based on these papers:\n\n{summaries}")
95
+ idea = call_llm(f"Propose a novel research direction based on this:\n\n{gap}")
96
+ paper = call_llm(f"""Write a detailed academic paper on "{topic}" using the idea:
97
+ {idea}
98
+ Structure:
99
+ 1. Abstract
100
+ 2. Introduction
101
+ 3. Related Work
102
+ 4. Methodology
103
+ 5. Experiments
104
+ 6. Results
105
+ 7. Conclusion
106
+ 8. References""")
107
+
108
+ st.subheader("🧠 Novel Idea")
109
+ st.markdown(idea)
110
+
111
+ st.subheader("πŸ“ Full Paper")
112
+ st.markdown(paper)
113
+
114
+ st.subheader("πŸ–ΌοΈ Diagram")
115
+ image_url = fetch_image_url(topic)
116
+ if image_url:
117
+ st.image(image_url, caption="Auto-selected diagram")
118
+
119
+ if st.button("πŸ“₯ Export to PDF"):
120
+ pdf = create_latex(topic, paper, image_url)
121
+ with open(pdf, "rb") as f:
122
+ st.download_button("Download PDF", f, file_name=f"{topic.replace(' ', '_')}.pdf")