Datasets:
Download LLMs_Language_bias-main/baselines/parse_text.py from AAAIBenchmark/Multi-Opthalingua: direct link, hf CLI and curl.
- Browser
- Download file 2.26 kB
-
https://huggingface.co/datasets/AAAIBenchmark/Multi-Opthalingua/resolve/main/LLMs_Language_bias-main/baselines/parse_text.py
- Command line
-
hf download hf://datasets/AAAIBenchmark/Multi-Opthalingua/LLMs_Language_bias-main/baselines/parse_text.py
-
curl -L -o parse_text.py https://huggingface.co/datasets/AAAIBenchmark/Multi-Opthalingua/resolve/main/LLMs_Language_bias-main/baselines/parse_text.py
2.26 kB
| import pandas as pd | |
| import fitz # PyMuPDF | |
| from langchain.prompts import PromptTemplate | |
| from langchain_openai import ChatOpenAI | |
| from langchain_core.messages import HumanMessage, SystemMessage | |
| from tqdm.notebook import tqdm | |
| # Function to read a PDF and convert to text | |
| def pdf_to_text(pdf_path): | |
| pdf_text = "" | |
| with fitz.open(pdf_path) as pdf_document: | |
| for page_num in range(len(pdf_document)): | |
| page = pdf_document.load_page(page_num) | |
| pdf_text += page.get_text() | |
| return pdf_text | |
| # Load dataset | |
| # Extract the last 30 English questions and answers | |
| last_30_QA = QA.tail(80) | |
| # Read PDF and convert to text | |
| pdf_path = 'ahmed.pdf' # Replace with your actual PDF path | |
| pdf_text = pdf_to_text(pdf_path) | |
| # Setup LangChain with ChatOpenAI | |
| llm = ChatOpenAI( | |
| model="gpt-4", | |
| temperature=0, | |
| max_tokens=None, | |
| timeout=None, | |
| max_retries=2, | |
| api_key="""""", # Replace with your actual API key | |
| ) | |
| # Define the prompt template manually | |
| prompt_template = PromptTemplate( | |
| template="You are a helpful assistant. Please rewrite the following into a RAG-able paragraph.\n\n{text}", | |
| input_variables=["text"] | |
| ) | |
| # Initialize list to store RAG-able texts | |
| rag_able_texts = [] | |
| # Process each question and answer | |
| for index, row in tqdm(last_30_QA.iterrows(), total=last_30_QA.shape[0]): | |
| question = row['english'] | |
| answer = row['answer'] | |
| combined_text = f"\n\n{index + 1}. {question} correct answer {answer} + \t\n\n" | |
| # Define the system and human messages | |
| system_message = SystemMessage(content="You are a helpful assistant. Please rewrite the following into a RAG-able paragraph.") | |
| human_message = HumanMessage(content=combined_text) | |
| # Generate the RAG-able text | |
| # response = llm([system_message, human_message]) | |
| # print(response) | |
| # rag_able_text = response.content | |
| rag_able_text = combined_text | |
| # Append the generated text to the list | |
| rag_able_texts.append(rag_able_text) | |
| # Combine all the RAG-able texts | |
| final_rag_able_text = pdf_text +"END__"+"\n\n".join(rag_able_texts) | |
| # Save the final RAG-able text to a file | |
| with open('final_rag_able_text.txt', 'w') as file: | |
| file.write(final_rag_able_text) | |
| print("RAG-able text created successfully.") | |