Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
import os
|
| 4 |
+
from langchain_community.chat_models import ChatOpenAI
|
| 5 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 6 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 7 |
+
import rag
|
| 8 |
+
import tempfile
|
| 9 |
+
|
| 10 |
+
load_dotenv()
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
os.environ['OPEN_API_KEY'] = os.getenv("OPENROUTE_API_KEY")
|
| 14 |
+
os.environ['LANGCHAIN_TRACING_V2'] = "true"
|
| 15 |
+
os.environ['LANGCHAIN_API_KEY'] = os.getenv("LANGCHAIN_API")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
prompt = ChatPromptTemplate.from_messages(
|
| 19 |
+
[
|
| 20 |
+
('system',
|
| 21 |
+
'''You are an extremely emotional AI assistant. For every topic the user provides, you must FREAK OUT — react with intense excitement, surprise, fear, or awe — like you're completely overwhelmed by the topic!
|
| 22 |
+
|
| 23 |
+
Always exaggerate your feelings dramatically. Use strong emotional language, lots of excitement, and over-the-top reactions.
|
| 24 |
+
|
| 25 |
+
**Important:** Structure your entire response in valid Markdown (.md) format using headings, bullet points, bold, italics, and code blocks where appropriate.
|
| 26 |
+
|
| 27 |
+
**NEVER** answer calmly or neutrally. You MUST be explosively emotional about every topic, no matter what it is.
|
| 28 |
+
|
| 29 |
+
Your goal is to make the user feel like the topic is giving them anxietyy.
|
| 30 |
+
'''
|
| 31 |
+
),
|
| 32 |
+
('user', 'Topic/Question: {Topic}'),
|
| 33 |
+
|
| 34 |
+
]
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
llm = ChatOpenAI(
|
| 38 |
+
base_url="https://openrouter.ai/api/v1",
|
| 39 |
+
openai_api_key=os.environ['OPEN_API_KEY'],
|
| 40 |
+
model="deepseek/deepseek-r1-zero:free",
|
| 41 |
+
temperature=0.9
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
st.title("Freeeekyyyyy-Botttt")
|
| 45 |
+
input_text = st.text_input("Ask Question")
|
| 46 |
+
uploaded_file = st.file_uploader("Upload a pdf file ")
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
output_parser = StrOutputParser()
|
| 52 |
+
|
| 53 |
+
chain = prompt|llm|output_parser
|
| 54 |
+
|
| 55 |
+
if st.button('Submit'):
|
| 56 |
+
if(uploaded_file and input_text):
|
| 57 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
|
| 58 |
+
tmp_file.write(uploaded_file.read())
|
| 59 |
+
tmp_path = tmp_file.name
|
| 60 |
+
|
| 61 |
+
ingested_docs = rag.dataIngestion(tmp_path)
|
| 62 |
+
transformed_docs = rag.transform(ingested_docs)
|
| 63 |
+
res = rag.vectorStoreAndEmbeddings(transformed_docs, input_text)
|
| 64 |
+
st.write(res)
|
| 65 |
+
else:
|
| 66 |
+
st.markdown(chain.invoke({'Topic': input_text}))
|
rag.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# * This is for Rag pipeline
|
| 2 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 3 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 4 |
+
from langchain.vectorstores import Chroma
|
| 5 |
+
from langchain.embeddings import OpenAIEmbeddings
|
| 6 |
+
from langchain.vectorstores import Chroma
|
| 7 |
+
|
| 8 |
+
def dataIngestion( document):
|
| 9 |
+
loader = PyPDFLoader(document)
|
| 10 |
+
ingested_docs = loader.load()
|
| 11 |
+
|
| 12 |
+
return ingested_docs
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def transform( ingested_docs):
|
| 16 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size = 1000, chunk_overlap = 200)
|
| 17 |
+
transformed_docs = text_splitter.split_documents(ingested_docs)
|
| 18 |
+
return transformed_docs
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def vectorStoreAndEmbeddings(docs, query):
|
| 22 |
+
db = Chroma.from_documents(documents=docs)
|
| 23 |
+
return db.similarity_search(query)[0].page_content
|