Baolina commited on
Commit
e463c73
·
verified ·
1 Parent(s): f728418

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -141
app.py DELETED
@@ -1,141 +0,0 @@
1
- import streamlit as st
2
- import os
3
- import json
4
- import requests
5
- from langchain_community.document_loaders import PyMuPDFLoader
6
- from openai import OpenAI
7
- import tiktoken
8
- import pandas as pd
9
- from langchain.text_splitter import RecursiveCharacterTextSplitter
10
- from langchain_community.embeddings.openai import OpenAIEmbeddings
11
- from langchain_community.vectorstores import Chroma
12
- import tempfile
13
-
14
-
15
- OPENAI_API_KEY = os.environ.get("API_KEY")
16
- OPENAI_API_BASE = os.environ.get("API_BASE")
17
-
18
- # Initialize OpenAI client
19
- client = OpenAI(
20
- api_key=OPENAI_API_KEY,
21
- base_url=OPENAI_API_BASE
22
- )
23
-
24
- # Define the system prompt for the model
25
- qna_system_message = """
26
- You are an AI assistant designed to support research teams in efficiently reviewing scientific literature. Your task is to provide evidence-based, concise, and relevant summaries based on the context provided from research papers.
27
-
28
- User input will include the necessary context for you to answer their questions. This context will begin with the token:
29
-
30
- ###Context
31
- The context contains excerpts from one or more research papers, along with associated metadata such as titles, authors, abstracts, keywords, and specific sections relevant to the query.
32
-
33
- When crafting your response
34
- -Use only the provided context to answer the question.
35
- -If the answer is found in the context, respond with concise and insight-focused summaries.
36
- -Include the paper title and, where applicable, arXiv ID or section reference as the source.
37
- -If the question is unrelated to the context or the context is empty, clearly respond with: "Sorry, this is out of my knowledge base."
38
-
39
-
40
- Please adhere to the following response guidelines:
41
- -Provide clear, direct answers using only the given context.
42
- -Do not include any additional information outside of the context.
43
- -Avoid rephrasing or generalizing unless explicitly relevant to the question.
44
- -If no relevant answer exists in the context, respond with: "Sorry, this is out of my knowledge base."
45
- -If the context is not provided, your response should also be: "Sorry, this is out of my knowledge base."
46
-
47
-
48
- Here is an example of how to structure your response:
49
-
50
- Answer:
51
- [Answer based on context]
52
-
53
- Source:
54
- [Source details with page or section]
55
- """
56
-
57
- # Define the user message template
58
- qna_user_message_template = """
59
- ###Context
60
- Here are some excerpts from GEN AI Research Paper and their sources that are relevant to the Gen AI question mentioned below:
61
- {context}
62
- ###Question
63
- {question}
64
- """
65
-
66
- @st.cache_resource
67
- def load_and_process_pdfs(uploaded_files):
68
- all_documents = []
69
- for uploaded_file in uploaded_files:
70
- with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
71
- tmp_file.write(uploaded_file.getvalue())
72
- tmp_file_path = tmp_file.name
73
- loader = PyMuPDFLoader(tmp_file_path)
74
- documents = loader.load()
75
- all_documents.extend(documents)
76
- os.remove(tmp_file_path) # Clean up the temporary file
77
- text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(
78
- encoding_name='cl100k_base',
79
- chunk_size=1000,
80
- )
81
- document_chunks = text_splitter.split_documents(all_documents)
82
-
83
- embedding_model = OpenAIEmbeddings(
84
- openai_api_key=OPENAI_API_KEY,
85
- openai_api_base=OPENAI_API_BASE
86
- )
87
-
88
- # Create an in-memory vector store (or use a persistent one if needed)
89
- vectorstore = Chroma.from_documents(
90
- document_chunks,
91
- embedding_model
92
- )
93
- return vectorstore.as_retriever(search_type='similarity', search_kwargs={'k': 5})
94
-
95
- def generate_rag_response(user_input, retriever, max_tokens=500, temperature=0, top_p=0.95):
96
- # Retrieve relevant document chunks
97
- relevant_document_chunks = retriever.get_relevant_documents(query=user_input)
98
- context_list = [d.page_content for d in relevant_document_chunks]
99
-
100
- # Combine document chunks into a single context
101
- context_for_query = ". ".join(context_list)
102
-
103
- user_message = qna_user_message_template.replace('{context}', context_for_query)
104
- user_message = user_message.replace('{question}', user_input)
105
-
106
- # Generate the response
107
- try:
108
- response = client.chat.completions.create(
109
- model="gpt-4o-mini",
110
- messages=[
111
- {"role": "system", "content": qna_system_message},
112
- {"role": "user", "content": user_message}
113
- ],
114
- max_tokens=max_tokens,
115
- temperature=temperature,
116
- top_p=top_p
117
- )
118
- response = response.choices[0].message.content.strip()
119
- except Exception as e:
120
- response = f'Sorry, I encountered the following error: \n {e}'
121
-
122
- return response
123
-
124
- # Streamlit App
125
- st.title("LLM-Powered Research Assistant")
126
-
127
- uploaded_files = st.file_uploader("Upload PDF files", type=["pdf"], accept_multiple_files=True)
128
-
129
- retriever = None
130
- if uploaded_files:
131
- st.info("Processing uploaded PDFs...")
132
- retriever = load_and_process_pdfs(uploaded_files)
133
- st.success("PDFs processed and ready for questioning!")
134
-
135
-
136
- if retriever:
137
- user_question = st.text_input("Ask a question about the uploaded documents:")
138
- if user_question:
139
- with st.spinner("Generating response..."):
140
- rag_response = generate_rag_response(user_question, retriever)
141
- st.write(rag_response)