Spaces:
Sleeping
Sleeping
Reestructure app.py
Browse files
app.py
CHANGED
|
@@ -12,24 +12,20 @@ from langchain_mistralai import ChatMistralAI
|
|
| 12 |
from langchain_community.document_loaders import WebBaseLoader
|
| 13 |
from langchain_core.rate_limiters import InMemoryRateLimiter
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
requests_per_second=0.1, # <-- MistralAI free. We can only make a request once every second
|
| 18 |
-
check_every_n_seconds=0.01, # Wake up every 100 ms to check whether allowed to make a request,
|
| 19 |
-
max_bucket_size=10, # Controls the maximum burst size.
|
| 20 |
-
)
|
| 21 |
|
| 22 |
-
# Get urls
|
| 23 |
-
urlsfile = open("urls.txt")
|
| 24 |
-
urls = urlsfile.readlines()
|
| 25 |
-
urls = [url.replace("\n","") for url in urls]
|
| 26 |
-
urlsfile.close()
|
| 27 |
|
| 28 |
-
# Load, chunk and index the contents of the blog.
|
| 29 |
-
loader = WebBaseLoader(urls)
|
| 30 |
-
docs = loader.load()
|
| 31 |
|
| 32 |
-
|
| 33 |
|
| 34 |
# Join content pages for processing
|
| 35 |
def format_docs(docs):
|
|
@@ -61,6 +57,17 @@ def RAG(llm, docs, embeddings):
|
|
| 61 |
|
| 62 |
return rag_chain
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
# LLM model
|
| 65 |
llm = ChatMistralAI(model="mistral-large-latest", rate_limiter=rate_limiter)
|
| 66 |
|
|
@@ -83,31 +90,34 @@ def handle_prompt(message, history):
|
|
| 83 |
except:
|
| 84 |
raise gr.Error("Requests rate limit exceeded")
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
# Define Gradio interface
|
| 103 |
-
demo = gr.ChatInterface(handle_prompt,
|
| 104 |
type="messages",
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
from langchain_community.document_loaders import WebBaseLoader
|
| 13 |
from langchain_core.rate_limiters import InMemoryRateLimiter
|
| 14 |
|
| 15 |
+
# Load documentation from urls
|
| 16 |
+
def get_docs():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Get urls
|
| 19 |
+
urlsfile = open("urls.txt")
|
| 20 |
+
urls = urlsfile.readlines()
|
| 21 |
+
urls = [url.replace("\n","") for url in urls]
|
| 22 |
+
urlsfile.close()
|
| 23 |
|
| 24 |
+
# Load, chunk and index the contents of the blog.
|
| 25 |
+
loader = WebBaseLoader(urls)
|
| 26 |
+
docs = loader.load()
|
| 27 |
|
| 28 |
+
return docs
|
| 29 |
|
| 30 |
# Join content pages for processing
|
| 31 |
def format_docs(docs):
|
|
|
|
| 57 |
|
| 58 |
return rag_chain
|
| 59 |
|
| 60 |
+
# Define a limiter to avoid rate limit issues with MistralAI
|
| 61 |
+
rate_limiter = InMemoryRateLimiter(
|
| 62 |
+
requests_per_second=0.1, # <-- MistralAI free. We can only make a request once every second
|
| 63 |
+
check_every_n_seconds=0.01, # Wake up every 100 ms to check whether allowed to make a request,
|
| 64 |
+
max_bucket_size=10, # Controls the maximum burst size.
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
# Get docs
|
| 68 |
+
docs = get_docs()
|
| 69 |
+
print("Pages loaded:",len(docs))
|
| 70 |
+
|
| 71 |
# LLM model
|
| 72 |
llm = ChatMistralAI(model="mistral-large-latest", rate_limiter=rate_limiter)
|
| 73 |
|
|
|
|
| 90 |
except:
|
| 91 |
raise gr.Error("Requests rate limit exceeded")
|
| 92 |
|
| 93 |
+
|
| 94 |
+
if __name__=="__main__":
|
| 95 |
+
|
| 96 |
+
# Predefined messages and examples
|
| 97 |
+
description = "AI powered assistant which answers any question related to the [CAMELS simulations](https://www.camel-simulations.org/)."
|
| 98 |
+
greetingsmessage = "Hi, I'm the CAMELS DocBot, I'm here to assist you with any question related to the CAMELS simulations."
|
| 99 |
+
example_questions = [
|
| 100 |
+
"How can I read a halo file?",
|
| 101 |
+
"Which simulation suites are included in CAMELS?",
|
| 102 |
+
"Which are the largest volumes in CAMELS simulations?",
|
| 103 |
+
"Write a complete snippet of code getting the power spectrum of a simulation"
|
| 104 |
+
]
|
| 105 |
+
|
| 106 |
+
# Define customized Gradio chatbot
|
| 107 |
+
chatbot = gr.Chatbot([{"role":"assistant", "content":greetingsmessage}],
|
|
|
|
|
|
|
|
|
|
| 108 |
type="messages",
|
| 109 |
+
avatar_images=["ims/userpic.png","ims/camelslogo.jpg"],
|
| 110 |
+
height="60vh")
|
| 111 |
+
|
| 112 |
+
# Define Gradio interface
|
| 113 |
+
demo = gr.ChatInterface(handle_prompt,
|
| 114 |
+
type="messages",
|
| 115 |
+
title="CAMELS DocBot",
|
| 116 |
+
fill_height=True,
|
| 117 |
+
examples=example_questions,
|
| 118 |
+
theme=gr.themes.Soft(),
|
| 119 |
+
description=description,
|
| 120 |
+
cache_examples=False,
|
| 121 |
+
chatbot=chatbot)
|
| 122 |
+
|
| 123 |
+
demo.launch()
|