forcemeow/rnd-analyzer
0
1import gradio as gr2import PyPDF23from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline4import torch5 6# Load Mistral (lightweight, fast, free)7model_id = "mistralai/Mistral-7B-Instruct-v0.1"8tokenizer = AutoTokenizer.from_pretrained(model_id)9model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")10generator = pipeline("text-generation", model=model, tokenizer=tokenizer)11 12# Extract text from PDF13def extract_text(pdf_file):14 text = ""15 reader = PyPDF2.PdfReader(pdf_file)16 for page in reader.pages:17 text += page.extract_text()18 return text19 20# Build prompt for LLM21def create_prompt(text):22 return f"""23You are an AI that analyzes research and development papers.24Please analyze the following R&D paper and provide:25 261. Abstract272. Key findings283. Keywords294. Feasibility of the idea305. Potential vulnerabilities or risks316. Innovative aspects32 33Paper Content:34\"\"\"35{text}36\"\"\"37"""38 39# Main function40def analyze(pdf_file):41 raw_text = extract_text(pdf_file)42 prompt = create_prompt(raw_text[:3000]) # limit text to avoid overload43 result = generator(prompt, max_length=1024, do_sample=True, temperature=0.7)[0]["generated_text"]44 return result45 46# Gradio UI47iface = gr.Interface(48 fn=analyze,49 inputs=gr.File(label="Upload your R&D Paper (PDF)", file_types=[".pdf"]),50 outputs=gr.Textbox(label="LLM Analysis Output"),51 title="R&D Analyzer - Powered by Mistral",52 description="This LLM reads your R&D paper and generates a summary, keywords, feasibility analysis, risks, and innovations."53)54 55iface.launch()56 