File size: 2,012 Bytes
35aab08
8519014
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
pip install gradio openai pdfplumber
import gradio as gr
import pdfplumber
import openai
import os

# Set your OpenAI API key here
openai.api_key = "YOUR_OPENAI_API_KEY"

# Function to extract text from uploaded PDF
def extract_text_from_pdf(pdf_file):
    text = ""
    with pdfplumber.open(pdf_file) as pdf:
        for page in pdf.pages:
            text += page.extract_text() + "\n"
    return text

# Function to generate critique using OpenAI LLM
def generate_critique(file):
    if file is None:
        return "Please upload a PDF file."

    # Extract text from PDF
    extracted_text = extract_text_from_pdf(file)

    # Truncate if too long for API (adjust depending on model token limit)
    if len(extracted_text) > 6000:
        extracted_text = extracted_text[:6000]

    # Prompt for LLM
    prompt = f"""
Analyze the following research paper and provide:

1. Section-wise summaries (Abstract, Introduction, Methodology, Results, Conclusion).
2. Identify potential research gaps or areas lacking clarity.
3. Suggest improvements to enhance the research quality.

Research Paper Content:
{extracted_text}
"""

    try:
        # Call OpenAI API
        response = openai.ChatCompletion.create(
            model="gpt-4",  # or "gpt-3.5-turbo" if you're on the free tier
            messages=[{"role": "user", "content": prompt}],
            max_tokens=1500,
            temperature=0.7
        )

        return response['choices'][0]['message']['content']
    
    except Exception as e:
        return f"Error: {str(e)}"

# Gradio Interface
iface = gr.Interface(
    fn=generate_critique,
    inputs=gr.File(label="Upload Research Paper (.pdf)", file_types=[".pdf"]),
    outputs=gr.Textbox(label="LLM Critique Output", lines=30),
    title="📄 Research Paper Critique Generator",
    description="Upload a research paper in PDF format. This tool will summarize each section, highlight research gaps, and suggest improvements using GPT-4."
)

# Launch on Hugging Face / Local
iface.launch()