0% found this document useful (0 votes)
11 views

NLP Exp 9 Outputs

Outputs for Exp 9 of NLP

Uploaded by

2021.rishi.kokil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

NLP Exp 9 Outputs

Outputs for Exp 9 of NLP

Uploaded by

2021.rishi.kokil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

pip install langchain langchain-google-genai PyPDF2 Flask

Show hidden output

keyboard_arrow_down Creating a PDF Reader for Reading the Documents


from PyPDF2 import PdfReader

def extract_text_from_pdf_arr(file_path):
reader = PdfReader(file_path)
text = []
for page in reader.pages:
page_words = []
# Extract text for each page and store in a list
for word in page.extract_text().splitlines(): # Split by lines
if word.strip(): # Avoid empty lines
page_words.append(word.strip()) # Add cleaned word
text.append(page_words) # Append words of each page
return text

# Beautify the multidimensional array out put


import pprint
pprint.pprint(extract_text_from_pdf_arr('/content/NLP Exp 8.pdf'))

Show hidden output

from langchain_google_genai import ChatGoogleGenerativeAI


from IPython.display import display, Markdown

# Set up Google API key


GOOGLE_API_KEY = "AIzaSyAImDC5BZ6bt73B3T-3zf1cE44aGuEqJ48"

# Initialize the LLM


llm = ChatGoogleGenerativeAI(
model="gemini-1.5-pro",
temperature=0.7,
max_tokens=150,
timeout=None,
max_retries=3,
google_api_key=GOOGLE_API_KEY
)

# Function to flatten the multidimensional array


def flatten_text(text_array):
return "\n".join(["\n".join(page) for page in text_array])

# Main loop for user interaction


while True:
# Ask for file input from the user
file_path = input("Enter the PDF file path (or 'exit' to quit): ")
if file_path.lower() == "exit":
print("Exiting chat...")
break

# Load PDF and extract text


try:
pdf_text = extract_text_from_pdf_arr(file_path) # Replace with your PDF file path
flat_pdf_text = flatten_text(pdf_text) # Flatten the extracted text
print("Context updated from the new file.")
except Exception as e:
print(f"Error reading the PDF: {str(e)}")
continue

# Chat interaction
while True:
user_input = input("You: ") # Take input from the user
if user_input.lower() == "exit":
print("Exiting chat...")
break

# Create the prompt by appending user input to the PDF content


prompt = f"Context: {flat_pdf_text}\nUser: {user_input}\nAssistant:"

# Get the response from the LLM


ai_response = llm.invoke([{"role": "user", "content": prompt}])

# P i t th AI' i M kd f b tt f tti
# Print the AI's response using Markdown for better formatting
display(Markdown(f"**You:** {user_input}"))
display(Markdown(f"**Chatbot:** {ai_response.content}"))

Enter the PDF file path (or 'exit' to quit): What is this document all about ?
Error reading the PDF: [Errno 2] No such file or directory: 'What is this document all about ?'
Enter the PDF file path (or 'exit' to quit): /content/NLP Exp 8.pdf
Context updated from the new file.
You: What is this document all about ?
You: What is this document all about ?
Chatbot: This document outlines an experiment using LangChain and a Large Language Model (LLM) like Gemini to automatically generate Multiple Choice
Questions (MCQs).
Here's a breakdown of the key takeaways:
Purpose: The experiment aims to demonstrate how to leverage LLMs for efficient and effective MCQ generation in educational settings.
Benefits: Automating MCQ generation can streamline assessments, provide varied and targeted questions, and offer valuable feedback to learners and
educators.
Techniques: The document details both basic and advanced prompt engineering techniques. This includes ensuring clarity and specificity in prompts,
focusing on relevant content, and customizing questions based on difficulty and target audience.
Quality Control: The document emphasizes the
You: So What is an LLM as mentioned in the Document
You: So What is an LLM as mentioned in the Document
Chatbot: This document talks about using Large Language Models (LLMs) like Gemini for MCQ generation.
In simpler terms, an LLM is a computer program that's really good at understanding and using human language. It's trained on massive amounts of text data,
allowing it to:
Understand the meaning of words and sentences.
Generate text that sounds like natural human language.
Answer questions, summarize information, and even write stories.
In the context of education, LLMs can be used to automate the creation of multiple-choice questions, making life easier for teachers and providing students with
varied and targeted assessments.
You: exit
Exiting chat...
Enter the PDF file path (or 'exit' to quit): exit
Exiting chat...

You might also like