File size: 1,429 Bytes
6c926cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pdfplumber
import openai
import os

# Set your OpenAI API key (set it in Hugging Face Secrets for security)
openai.api_key = os.getenv("AIzaSyB9xXfstwZf8htC-lTjD3nwCHChbh-zWtg")
client = openai.OpenAI()

def analyze_pdf(pdf_file):
    # Extract text from PDF
    with pdfplumber.open(pdf_file) as pdf:
        text = "\n".join([page.extract_text() or "" for page in pdf.pages])
    
    if not text.strip():
        return "No readable text found in the PDF."

    # Build the prompt
    prompt = f"""Analyze the following research paper content:
{text}
Please:
1. Summarize each section of the paper.
2. Identify any potential research gaps.
3. Provide constructive suggestions for improving the paper."""

    # Send to OpenAI
    response = client.chat.completions.create(
        model="gpt-3.5-turbo",  # or "gpt-4"
        messages=[
            {"role": "user", "content": prompt}
        ],
        temperature=0.7,
        max_tokens=2000
    )

    result = response.choices[0].message.content
    return result

# Gradio UI
iface = gr.Interface(
    fn=analyze_pdf,
    inputs=gr.File(label="Upload Research Paper (PDF)", file_types=[".pdf"]),
    outputs=gr.Textbox(label="Critique Output", lines=30),
    title="🧠 Research Paper Critique Generator",
    description="Upload a research paper PDF to get summaries, research gaps, and improvement suggestions using GPT."
)

iface.launch()