Spaces:
Runtime error
Runtime error
| 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() | |