Spaces:
Sleeping
Sleeping
Create code.py
Browse files
code.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pdfplumber
|
| 3 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Load a local or small language model (you can change this to something better if your machine can handle it)
|
| 7 |
+
model_name = "gpt2" # Replace with a better model if desired, like 'tiiuae/falcon-rw-1b'
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 10 |
+
generator = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0 if torch.cuda.is_available() else -1)
|
| 11 |
+
|
| 12 |
+
def extract_text_from_pdf(pdf_file):
|
| 13 |
+
text = ""
|
| 14 |
+
with pdfplumber.open(pdf_file) as pdf:
|
| 15 |
+
for page in pdf.pages:
|
| 16 |
+
page_text = page.extract_text()
|
| 17 |
+
if page_text:
|
| 18 |
+
text += page_text + "\n"
|
| 19 |
+
return text
|
| 20 |
+
|
| 21 |
+
def generate_critique(file):
|
| 22 |
+
if file is None:
|
| 23 |
+
return "Please upload a PDF file."
|
| 24 |
+
|
| 25 |
+
extracted_text = extract_text_from_pdf(file)
|
| 26 |
+
|
| 27 |
+
# Truncate text if too long for the model
|
| 28 |
+
extracted_text = extracted_text[:1500]
|
| 29 |
+
|
| 30 |
+
prompt = f"""
|
| 31 |
+
Analyze the following research paper and provide:
|
| 32 |
+
1. give critique for the research paper
|
| 33 |
+
2. give the critique report in points
|
| 34 |
+
3. Section-wise summaries (Abstract, Introduction, Methodology, Results, Conclusion).
|
| 35 |
+
4. Identify potential research gaps or areas lacking clarity.
|
| 36 |
+
5. Suggest improvements to enhance the research quality.
|
| 37 |
+
6. should be short, crisp and precise
|
| 38 |
+
Research Paper Content:
|
| 39 |
+
{extracted_text}
|
| 40 |
+
Critique:
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
try:
|
| 44 |
+
response = generator(prompt, max_length=1024, do_sample=True, temperature=0.7)[0]["generated_text"]
|
| 45 |
+
# Trim to only return what's after "Critique:"
|
| 46 |
+
return response.split("Critique:")[-1].strip()
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"Error: {str(e)}"
|
| 49 |
+
|
| 50 |
+
iface = gr.Interface(
|
| 51 |
+
fn=generate_critique,
|
| 52 |
+
inputs=gr.File(label="Upload PDF"),
|
| 53 |
+
outputs=gr.Textbox(label="Critique Output", lines=30),
|
| 54 |
+
title="📄 Research Paper Critique Generator (BY JAYANTH)",
|
| 55 |
+
description="Upload a research paper PDF and receive a basic AI-generated critique using a local model."
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
iface.launch()
|