Notes
Notes - notes.io |
# ***********************************************
# -------------- to check openai working or not -------------------
# import openai
# print(openai.__version__)
# --------------- Working and able to answers in terminal ------------------
# from openai import OpenAI
# client = OpenAI(
# api_key="sutra_z9CdUwEB6jBZwr9H4CpBwDVE7I5mG531gSIryR8v5qiVvfiUUnKGujUZI5n3",
# base_url="https://api.two.ai/v2"
# )
# response = client.chat.completions.create(
# model="sutra-v2",
# messages=[
# {"role": "system", "content": "Give me 200 words of answers"},
# {"role": "user", "content": "What is today's top news?"}
# ],
# max_tokens=1024,
# temperature=0.7
# )
# print(response.choices[0].message.content)
# ----------------- extraction with gemini (Working Properly without using RAG)------------------
# from openai import OpenAI
# import fitz # read & manipulate pdf (part of PyMuPDF library)
# client = OpenAI(
# api_key="sutra_z9CdUwEB6jBZwr9H4CpBwDVE7I5mG531gSIryR8v5qiVvfiUUnKGujUZI5n3",
# base_url="https://api.two.ai/v2"
# )
# def extract_text_from_pdf(pdf_path, txt_path):
# """Extracts text from a PDF file and saves it to a .txt file."""
# extracted_text = ""
# with fitz.open(pdf_path) as pdf:
# for page in pdf:
# text = page.get_text()
# if text:
# lines = text.split('n')
# for line in lines:
# cleaned_line = line.strip()
# if cleaned_line:
# extracted_text += cleaned_line + 'n'
# with open(txt_path, 'w', encoding='utf-8') as f:
# f.write(extracted_text)
# def load_extracted_text(txt_path):
# """Loads text from a .txt file."""
# with open(txt_path, 'r', encoding='utf-8') as f:
# return f.read()
# def ask_openai(question, context):
# """Asks a question to OpenAI API with the provided context."""
# response = client.chat.completions.create(
# model="sutra-v2",
# messages=[
# {"role": "system", "content": "You are an assistant that provides answers based on the given context."},
# {"role": "user", "content": f"{context}nnQuestion: {question}"}
# ],
# max_tokens=1024,
# temperature=0.7
# )
# return response.choices[0].message.content
# PDF_FILE_NAME = 'fivepages.pdf'
# TXT_FILE_NAME = 'output4.txt'
# extract_text_from_pdf(PDF_FILE_NAME, TXT_FILE_NAME)
# extracted_text = load_extracted_text(TXT_FILE_NAME)
# user_question = "what is Affidavits.?"
# answer = ask_openai(user_question, extracted_text)
# print(answer)
# ------------------------------- Working Properly ------------------------
# ########################## rag (Retrieval Augmented Generation) ##############################
# from openai import OpenAI
# import fitz # read & manipulate pdf (part of PyMuPDF library)
# def extract_text_from_pdf(pdf_path):
# """Extracts text from a PDF file and returns it as a string."""
# extracted_text = ""
# with fitz.open(pdf_path) as pdf:
# for page in pdf:
# text = page.get_text()
# if text:
# lines = text.split('n')
# for line in lines:
# cleaned_line = line.strip()
# if cleaned_line:
# extracted_text += cleaned_line + 'n'
# return extracted_text
# def get_response_from_openai(question, context):
# """Asks a question to OpenAI API with the provided context."""
# client = OpenAI(
# api_key="sutra_z9CdUwEB6jBZwr9H4CpBwDVE7I5mG531gSIryR8v5qiVvfiUUnKGujUZI5n3",
# base_url="https://api.two.ai/v2"
# )
# response = client.chat.completions.create(
# model="sutra-v2",
# messages=[
# {"role": "system", "content": "Use the provided context for answering questions."},
# {"role": "user", "content": f"{context}nnQuestion: {question}"}
# ],
# max_tokens=1024,
# temperature=0.7
# )
# return response.choices[0].message.content
# PDF_FILE_NAME = 'fivepages.pdf'
# extracted_text = extract_text_from_pdf(PDF_FILE_NAME)
# user_question = "what is 107.6.2?please answer as it is, dont modify"
# response = get_response_from_openai(user_question, extracted_text)
# print(response)
# how many sections are there?
# how many sections are there? mention names
# what is section 106?
# what is section 106?please do not modify, i want answer as it is
# what is submittal documents?
# what is submittal documents? and how many articles in this
# what is 107.6.2?"
# what is 107.6.2?please answer as it is, dont modify"
# ------------------------ in Terminal taking user_question and gave response; Run in terminal:(python geminiAPI.py)-----------------
# -------------------------- Single question at a time -------------------------------
# from openai import OpenAI
# import fitz # read & manipulate pdf (part of PyMuPDF library)
# def extract_text_from_pdf(pdf_path):
# """Extracts text from a PDF file and returns it as a string."""
# extracted_text = ""
# with fitz.open(pdf_path) as pdf:
# for page in pdf:
# text = page.get_text()
# if text:
# lines = text.split('n')
# for line in lines:
# cleaned_line = line.strip()
# if cleaned_line:
# extracted_text += cleaned_line + 'n'
# return extracted_text
# def get_response_from_openai(question, context):
# """Asks a question to OpenAI API with the provided context."""
# client = OpenAI(
# api_key="sutra_z9CdUwEB6jBZwr9H4CpBwDVE7I5mG531gSIryR8v5qiVvfiUUnKGujUZI5n3",
# base_url="https://api.two.ai/v2"
# )
# response = client.chat.completions.create(
# model="sutra-v2",
# messages=[
# {"role": "system", "content": "Use the provided context for answering questions."},
# {"role": "user", "content": f"{context}nnQuestion: {question}"}
# ],
# max_tokens=1024,
# temperature=0.7
# )
# return response.choices[0].message.content
# PDF_FILE_NAME = 'fivepages.pdf'
# extracted_text = extract_text_from_pdf(PDF_FILE_NAME)
# user_question = input("Please enter your question: ")
# response = get_response_from_openai(user_question, extracted_text)
# print(response)
# ------------------------ Multiple question (Sir Code, but issue is i am not getting answers from pdf)
# from openai import OpenAI
# # import fitz # read & manipulate pdf (part of PyMuPDF library)
# # def extract_text_from_pdf(pdf_path):
# # """Extracts text from a PDF file and returns it as a string."""
# # extracted_text = ""
# # with fitz.open(pdf_path) as pdf:
# # for page in pdf:
# # text = page.get_text()
# # if text:
# # lines = text.split('n')
# # for line in lines:
# # cleaned_line = line.strip()
# # if cleaned_line:
# # extracted_text += cleaned_line + 'n'
# # return extracted_text
# def get_response_from_openai(messages):
# """Asks a question to OpenAI API with the provided context."""
# client = OpenAI(
# api_key="sutra_z9CdUwEB6jBZwr9H4CpBwDVE7I5mG531gSIryR8v5qiVvfiUUnKGujUZI5n3",
# base_url="https://api.two.ai/v2"
# )
# response = client.chat.completions.create(
# model="sutra-v2",
# messages=messages,
# max_tokens=1024,
# temperature=0.7
# )
# return response.choices[0].message.content
# messages = [
# {"role":"system", "content":"You are an assistant whose name is ChatGemini that provides answers based on the given context. Feel free to ask me anything tech-related or just share a joke!"}
# ]
# while True:
# user_question = input("Please enter your question (or type 'exit' to quit): ")
# messages.append({"role":"user", "content":user_question})
# if user_question.lower() == 'exit' or user_question.lower() == 'quit':
# print("Exiting the program.")
# break
# messages.append({"role": "user", "content": user_question})
# response = get_response_from_openai(messages)
# messages.append({"role": "assistant", "content": response})
# print(response)
# -----------------------------Sir code, using streamlit
# import streamlit as st
# from openai import OpenAI
# def get_response_from_openai(messages):
# """Asks a question to OpenAI API with the provided context."""
# client = OpenAI(
# api_key="sutra_z9CdUwEB6jBZwr9H4CpBwDVE7I5mG531gSIryR8v5qiVvfiUUnKGujUZI5n3",
# base_url="https://api.two.ai/v2"
# )
# response = client.chat.completions.create(
# model="sutra-v2",
# messages=messages,
# max_tokens=1024,
# temperature=0.7
# )
# return response.choices[0].message.content
# st.title("ChatGemini: Your AI Assistant")
# if 'messages' not in st.session_state:
# st.session_state.messages = [
# {"role": "system", "content": "You are an assistant whose name is ChatGemini that provides answers based on the given context. Feel free to ask me anything tech-related or just share a joke!"}
# ]
# user_question = st.text_input("Please enter your question:")
# if st.button("Ask"):
# if user_question:
# st.session_state.messages.append({"role": "user", "content": user_question})
# response = get_response_from_openai(st.session_state.messages)
# st.session_state.messages.append({"role": "assistant", "content": response})
# st.write("Response:", response)
# if st.session_state.messages:
# st.subheader("Chat History")
# for msg in st.session_state.messages:
# if msg["role"] == "user":
# st.markdown(f"**You:** {msg['content']}")
# else:
# st.markdown(f"**ChatGemini:** {msg['content']}")
# ----------------------------- i have added pdf in sir code
# from openai import OpenAI
# import fitz # read & manipulate pdf (part of PyMuPDF library)
# def extract_text_from_pdf(pdf_path):
# """Extracts text from a PDF file and returns it as a string."""
# extracted_text = ""
# with fitz.open(pdf_path) as pdf:
# for page in pdf:
# text = page.get_text()
# if text:
# lines = text.split('n')
# for line in lines:
# cleaned_line = line.strip()
# if cleaned_line:
# extracted_text += cleaned_line + 'n'
# return extracted_text
# def get_response_from_openai(messages):
# """Asks a question to OpenAI API with the provided context."""
# client = OpenAI(
# api_key="sutra_z9CdUwEB6jBZwr9H4CpBwDVE7I5mG531gSIryR8v5qiVvfiUUnKGujUZI5n3",
# base_url="https://api.two.ai/v2"
# )
# response = client.chat.completions.create(
# model="sutra-v2",
# messages=messages,
# max_tokens=1024,
# temperature=0.7
# )
# return response.choices[0].message.content
# PDF_FILE_NAME = 'fivepages.pdf'
# extracted_text = extract_text_from_pdf(PDF_FILE_NAME)
# messages = [
# {"role": "system", "content": "You are an assistant whose name is ChatGemini that provides answers based on the given context. Feel free to ask me anything tech-related or just share a joke!"},
# {"role": "assistant", "content": extracted_text} # Add the extracted text as context
# ]
# while True:
# user_question = input("Please enter your question (or type 'exit' to quit): ")
# if user_question.lower() == 'exit' or user_question.lower() == 'quit':
# print("Exiting the program.")
# break
# messages.append({"role": "user", "content": user_question})
# response = get_response_from_openai(messages)
# messages.append({"role": "assistant", "content": response})
# print(response)
# ---------------------------- streamlit (issue with extracting all text on UI page)
# import streamlit as st
# from openai import OpenAI
# import fitz # read & manipulate pdf (part of PyMuPDF library)
# def extract_text_from_pdf(pdf_path):
# """Extracts text from a PDF file and returns it as a string."""
# extracted_text = ""
# with fitz.open(pdf_path) as pdf:
# for page in pdf:
# text = page.get_text()
# if text:
# lines = text.split('n')
# for line in lines:
# cleaned_line = line.strip()
# if cleaned_line:
# extracted_text += cleaned_line + 'n'
# return extracted_text
# def get_response_from_openai(messages):
# """Asks a question to OpenAI API with the provided context."""
# client = OpenAI(
# api_key="sutra_z9CdUwEB6jBZwr9H4CpBwDVE7I5mG531gSIryR8v5qiVvfiUUnKGujUZI5n3",
# base_url="https://api.two.ai/v2"
# )
# response = client.chat.completions.create(
# model="sutra-v2",
# messages=messages,
# max_tokens=1024,
# temperature=0.7
# )
# return response.choices[0].message.content
# st.title("ChatGemini: Your AI Assistant")
# pdf_file = st.file_uploader("Upload a PDF file", type=["pdf"])
# if pdf_file is not None:
# extracted_text = extract_text_from_pdf(pdf_file)
# if 'messages' not in st.session_state:
# st.session_state.messages = [
# {"role": "system", "content": "You are an assistant whose name is ChatGemini that provides answers based on the given context. Feel free to ask me anything tech-related or just share a joke!"},
# {"role": "assistant", "content": extracted_text}
# ]
# user_question = st.text_input("Please enter your question:")
# if st.button("Ask"):
# if user_question:
# st.session_state.messages.append({"role": "user", "content": user_question})
# response = get_response_from_openai(st.session_state.messages)
# st.session_state.messages.append({"role": "assistant", "content": response})
# st.write("Response:", response)
# if st.session_state.messages:
# st.subheader("Chat History")
# for msg in st.session_state.messages:
# if msg["role"] == "user":
# st.markdown(f"**You:** {msg['content']}")
# else:
# st.markdown(f"**ChatGemini:** {msg['content']}")
# else:
# st.warning("Please upload a PDF file to start.")
# ---------- As expected i made "chat with pdf" but it will not work for large pdf, solution : chunks
import openai
import streamlit as st
from openai import OpenAI
import fitz # read & manipulate pdf (part of PyMuPDF library)
def extract_text_from_pdf(pdf_path):
"""Extracts text from a PDF file and returns it as a string."""
extracted_text = ""
with fitz.open(pdf_path) as pdf:
for page in pdf:
text = page.get_text()
if text:
lines = text.split('n')
for line in lines:
cleaned_line = line.strip()
if cleaned_line:
extracted_text += cleaned_line + 'n'
return extracted_text
def get_response_from_openai(messages):
"""Asks a question to OpenAI API with the provided context."""
try:
client = OpenAI(
api_key="sutra_Oi0EsE1B4wLKoZJb98bxXsuvQ5GIJfoinydV3MPWymlcOs5qWPC1T8ayCQRZ", # Replace with your actual API key
base_url="https://api.two.ai/v2"
)
response = client.chat.completions.create(
model="sutra-v2",
messages=messages,
max_tokens=1024,
temperature=0.7
)
return response.choices[0].message.content
except openai.AuthenticationError as e:
st.error("Authentication error: No credits remaining. Please check your account.")
return None
except Exception as e:
st.error(f"An error occurred: {str(e)}")
return None
st.title("ChatGemini: Your AI Assistant")
pdf_file = st.file_uploader("Upload a PDF file", type=["pdf"])
if pdf_file is not None:
extracted_text = extract_text_from_pdf(pdf_file)
if 'messages' not in st.session_state:
st.session_state.messages = [
{"role": "system", "content": "You are ChatGemini, an assistant providing context-based answers. Ask anything tech-related or share a joke!"},
{"role": "assistant", "content": extracted_text}
]
col1, col2 = st.columns(2)
with col1:
st.subheader("PDF Text Extraction")
st.text_area("Extracted Text from PDF:", value=extracted_text, height=300)
with col2:
st.subheader("Question & Answering")
user_question = st.text_input("Please enter your question:")
if st.button("Ask"):
if user_question:
st.session_state.messages.append({"role": "user", "content": user_question})
response = get_response_from_openai(st.session_state.messages)
st.session_state.messages.append({"role": "assistant", "content": response})
st.write("Response:", response)
if len(st.session_state.messages) > 2:
st.subheader("Chat History")
for msg in st.session_state.messages:
if msg["role"] == "user" or (msg["role"] == "assistant" and msg["content"] != extracted_text):
st.markdown(f"**{msg['role'].capitalize()}:** {msg['content']}")
else:
st.warning("Please upload a PDF file to start.")
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team
