Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1 +1,66 @@
|
|
| 1 |
pip install gradio openai pdfplumber
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
pip install gradio openai pdfplumber
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import pdfplumber
|
| 4 |
+
import openai
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Set your OpenAI API key here
|
| 8 |
+
openai.api_key = "YOUR_OPENAI_API_KEY"
|
| 9 |
+
|
| 10 |
+
# Function to extract text from uploaded PDF
|
| 11 |
+
def extract_text_from_pdf(pdf_file):
|
| 12 |
+
text = ""
|
| 13 |
+
with pdfplumber.open(pdf_file) as pdf:
|
| 14 |
+
for page in pdf.pages:
|
| 15 |
+
text += page.extract_text() + "\n"
|
| 16 |
+
return text
|
| 17 |
+
|
| 18 |
+
# Function to generate critique using OpenAI LLM
|
| 19 |
+
def generate_critique(file):
|
| 20 |
+
if file is None:
|
| 21 |
+
return "Please upload a PDF file."
|
| 22 |
+
|
| 23 |
+
# Extract text from PDF
|
| 24 |
+
extracted_text = extract_text_from_pdf(file)
|
| 25 |
+
|
| 26 |
+
# Truncate if too long for API (adjust depending on model token limit)
|
| 27 |
+
if len(extracted_text) > 6000:
|
| 28 |
+
extracted_text = extracted_text[:6000]
|
| 29 |
+
|
| 30 |
+
# Prompt for LLM
|
| 31 |
+
prompt = f"""
|
| 32 |
+
Analyze the following research paper and provide:
|
| 33 |
+
|
| 34 |
+
1. Section-wise summaries (Abstract, Introduction, Methodology, Results, Conclusion).
|
| 35 |
+
2. Identify potential research gaps or areas lacking clarity.
|
| 36 |
+
3. Suggest improvements to enhance the research quality.
|
| 37 |
+
|
| 38 |
+
Research Paper Content:
|
| 39 |
+
{extracted_text}
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
# Call OpenAI API
|
| 44 |
+
response = openai.ChatCompletion.create(
|
| 45 |
+
model="gpt-4", # or "gpt-3.5-turbo" if you're on the free tier
|
| 46 |
+
messages=[{"role": "user", "content": prompt}],
|
| 47 |
+
max_tokens=1500,
|
| 48 |
+
temperature=0.7
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
return response['choices'][0]['message']['content']
|
| 52 |
+
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return f"Error: {str(e)}"
|
| 55 |
+
|
| 56 |
+
# Gradio Interface
|
| 57 |
+
iface = gr.Interface(
|
| 58 |
+
fn=generate_critique,
|
| 59 |
+
inputs=gr.File(label="Upload Research Paper (.pdf)", file_types=[".pdf"]),
|
| 60 |
+
outputs=gr.Textbox(label="LLM Critique Output", lines=30),
|
| 61 |
+
title="📄 Research Paper Critique Generator",
|
| 62 |
+
description="Upload a research paper in PDF format. This tool will summarize each section, highlight research gaps, and suggest improvements using GPT-4."
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
# Launch on Hugging Face / Local
|
| 66 |
+
iface.launch()
|