Download app.py from Pezh/Saro: direct link, hf CLI and curl.
- Browser
- Download file 4.99 kB
-
https://huggingface.co/spaces/Pezh/Saro/resolve/main/app.py
- Command line
-
hf download hf://spaces/Pezh/Saro/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Pezh/Saro/resolve/main/app.py
4.99 kB
| import streamlit as st | |
| import fitz | |
| import io | |
| import PyPDF2 | |
| from PyPDF2 import PdfReader | |
| from langchain.text_splitter import RecursiveCharacterTextSplitter | |
| import google.generativeai as genai | |
| #from langchain import FAISS, GoogleGenerativeAI, PdfReader | |
| from langchain_community.vectorstores import FAISS | |
| #from langchain.google_genai import ChatGoogleGenerativeAI | |
| from langchain.chains.question_answering import load_qa_chain | |
| from langchain.prompts import PromptTemplate | |
| from langchain_google_genai import GoogleGenerativeAIEmbeddings, GoogleGenerativeAI, ChatGoogleGenerativeAI | |
| #from langchain import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI, GoogleGenerativeAI | |
| #from langchain.google_genai import GoogleGenerativeAIEmbeddings | |
| #from langchain_google_genai.chat_models import ChatGoogleGenerativeAI | |
| #from langchain_google_genai.embeddings import GoogleGenerativeAIEmbeddings | |
| #from langchain_google_genai.llms import GoogleGenerativeAI | |
| #from dotenv import load_dotenv | |
| #import os | |
| #load_dotenv() | |
| #GOOGLE_API_KEY = "AIzaSyBd36RWeqDpLur3E7TTlX3wnyIh_rdhsU8" | |
| genai.configure(api_key="GOOGLE_API_KEY")#os.getenv("GOOGLE_API_KEY")) | |
| def get_pdf_text(pdf_docs): | |
| text = "" | |
| for pdf in pdf_docs: | |
| pdf_reader = PdfReader(pdf) | |
| for page in pdf_reader.pages: | |
| text += page.extract_text() | |
| return text | |
| def get_text_chunks(text): | |
| text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000) | |
| chunks = text_splitter.split_text(text) | |
| return chunks | |
| def get_vector_store(text_chunks): | |
| embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")#, google_api_key="AIzaSyBd36RWeqDpLur3E7TTlX3wnyIh_rdhsU8") | |
| # | |
| vector_store = FAISS.from_texts(texts=text_chunks, embedding=embeddings) | |
| vector_store.save_local("faiss_index") | |
| def get_conversation_chain(): | |
| prompt_template = """ | |
| Answer the question clear and precise. If not provided the context return the result as | |
| "Sorry I dont know the answer", don't provide the wrong answer. | |
| Context:\n {context}?\n | |
| Question:\n{question}\n | |
| Answer: | |
| """ | |
| llm = GoogleGenerativeAI(model="gemini-pro", temperature=0.1) # Create the LLM model instance | |
| model = load_qa_chain( | |
| llm=llm, # Pass the LLM model instance to the load_qa_chain call | |
| chain_type='stuff', # Confirm the 'chain_type' is correct or update accordingly | |
| prompt=PromptTemplate(template=prompt_template, input_variables=['context', 'question']) | |
| ) | |
| return model | |
| #def main(): | |
| st.title("Chat App with Multiple PDFs") | |
| st.write("Please select the PDF files") | |
| #pdf_files = st.file_uploader("Upload PDF", type=["pdf"], accept_multiple_files=False) | |
| user_question = st.text_input("Enter your question") | |
| genai.configure(api_key="AIzaSyBd36RWeqDpLur3E7TTlX3wnyIh_rdhsU8") | |
| # Set up the model | |
| generation_config = { | |
| "temperature": 0.9, | |
| "top_p": 1, | |
| "top_k": 1, | |
| "max_output_tokens": 2048, | |
| } | |
| safety_settings = [ | |
| { | |
| "category": "HARM_CATEGORY_HARASSMENT", | |
| "threshold": "BLOCK_MEDIUM_AND_ABOVE" | |
| }, | |
| { | |
| "category": "HARM_CATEGORY_HATE_SPEECH", | |
| "threshold": "BLOCK_MEDIUM_AND_ABOVE" | |
| }, | |
| { | |
| "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", | |
| "threshold": "BLOCK_MEDIUM_AND_ABOVE" | |
| }, | |
| { | |
| "category": "HARM_CATEGORY_DANGEROUS_CONTENT", | |
| "threshold": "BLOCK_MEDIUM_AND_ABOVE" | |
| }, | |
| ] | |
| text_input = st.text_input('سوالتان را در مورد علایم، داروها، عوارض و یا آزمایشات دیابت بپرسید', placeholder='من بیمار مبتلا به دیابت و سکته قلبی هستم و از متفورمین و پیوگلیتازون مصرف میکنم، این داروهارو چجوری مصرف کنم و آیا عوارضی خواهند داشت با توجه به بسکته قلبی که داشتهام؟') | |
| model = genai.GenerativeModel(model_name="gemini-pro", | |
| generation_config=generation_config, | |
| safety_settings=safety_settings) | |
| #prompt_parts = [text_input] | |
| #response = model.generate_content(prompt_parts) | |
| l=[] | |
| #l.append(response) | |
| if 3 > 2: | |
| #pdf_docs = [PdfReader(pdf) for pdf in pdf_files]#PdfReader(io.BytesIO(pdf.read())) for pdf in pdf_files] | |
| #text = get_pdf_text(pdf_docs) | |
| pdf_text = "" | |
| document = fitz.open('harrisons-manual-of-medicine-20th-edition-9781260455359-9781260455342_compress.pdf') | |
| for page_number in range(document.page_count): | |
| page = document.load_page(page_number) | |
| pdf_text += page.get_text() | |
| #pdf_text = "" | |
| #with open('ex.pdf', "rb") as file: | |
| # reader = PyPDF2.PdfReader(file) | |
| # for page_number in range(len(reader.pages)): | |
| # pdf_text += reader.pages[page_number].extract_text() | |
| text = pdf_text | |
| prompt_parts = [text_input + text] | |
| response = model.generate_content(prompt_parts) | |
| l.append(response) | |
| st.write(l[0]) | |