Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +148 -0
- requirements.txt +9 -0
app.py
ADDED
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import necessary libraries
|
2 |
+
import nest_asyncio
|
3 |
+
import gradio as gr
|
4 |
+
import requests
|
5 |
+
from bs4 import BeautifulSoup
|
6 |
+
from huggingface_hub import InferenceClient
|
7 |
+
from langchain.chains import RAGChain, RunnablePassthrough, LLMChain
|
8 |
+
from langchain.retrievers import FaissRetriever
|
9 |
+
from langchain.prompts import PromptTemplate
|
10 |
+
from langchain.wrappers import HuggingFacePipeline
|
11 |
+
from langchain.indexing import AsyncChromiumLoader, Html2TextTransformer, CharacterTextSplitter, FAISS, HuggingFaceEmbeddings
|
12 |
+
|
13 |
+
# Apply nest_asyncio for asynchronous operations in environments like Jupyter notebooks
|
14 |
+
nest_asyncio.apply()
|
15 |
+
|
16 |
+
# Initialize the InferenceClient with the specified model
|
17 |
+
client = InferenceClient(
|
18 |
+
"mistralai/Mistral-7B-Instruct-v0.1"
|
19 |
+
)
|
20 |
+
|
21 |
+
# Set up a prompt template for the model (customize as needed)
|
22 |
+
prompt_template = PromptTemplate()
|
23 |
+
|
24 |
+
# Define the list of articles to index
|
25 |
+
articles = [
|
26 |
+
"https://www.fantasypros.com/2023/11/rival-fantasy-nfl-week-10/",
|
27 |
+
"https://www.fantasypros.com/2023/11/5-stats-to-know-before-setting-your-fantasy-lineup-week-10/",
|
28 |
+
"https://www.fantasypros.com/2023/11/nfl-week-10-sleeper-picks-player-predictions-2023/",
|
29 |
+
"https://www.fantasypros.com/2023/11/nfl-dfs-week-10-stacking-advice-picks-2023-fantasy-football/",
|
30 |
+
"https://www.fantasypros.com/2023/11/players-to-buy-low-sell-high-trade-advice-2023-fantasy-football/"
|
31 |
+
]
|
32 |
+
|
33 |
+
# Scrapes the blogs above
|
34 |
+
loader = AsyncChromiumLoader(articles)
|
35 |
+
docs = loader.load()
|
36 |
+
|
37 |
+
# Converts HTML to plain text
|
38 |
+
html2text = Html2TextTransformer()
|
39 |
+
docs_transformed = html2text.transform_documents(docs)
|
40 |
+
|
41 |
+
# Chunk text
|
42 |
+
text_splitter = CharacterTextSplitter(chunk_size=100,
|
43 |
+
chunk_overlap=10)
|
44 |
+
chunked_documents = text_splitter.split_documents(docs_transformed)
|
45 |
+
|
46 |
+
# Load chunked documents into the FAISS index
|
47 |
+
db = FAISS.from_documents(chunked_documents,
|
48 |
+
HuggingFaceEmbeddings(model_name='sentence-transformers/all-mpnet-base-v2'))
|
49 |
+
|
50 |
+
retriever = db.as_retriever()
|
51 |
+
|
52 |
+
# Create the RAG chain by combining the language model with the retriever
|
53 |
+
rag_chain = ({"context": retriever, "question": RunnablePassthrough()} | LLMChain)
|
54 |
+
|
55 |
+
# Define the generation function for the Gradio interface
|
56 |
+
def generate(
|
57 |
+
prompt, history, temperature=0.7, max_new_tokens=256, top_p=0.95, repetition_penalty=1.1,
|
58 |
+
):
|
59 |
+
temperature = float(temperature)
|
60 |
+
if temperature < 1e-2:
|
61 |
+
temperature = 1e-2
|
62 |
+
top_p = float(top_p)
|
63 |
+
|
64 |
+
generate_kwargs = dict(
|
65 |
+
temperature=temperature,
|
66 |
+
max_new_tokens=max_new_tokens,
|
67 |
+
top_p=top_p,
|
68 |
+
repetition_penalty=repetition_penalty,
|
69 |
+
do_sample=True,
|
70 |
+
seed=42,
|
71 |
+
)
|
72 |
+
|
73 |
+
formatted_prompt = "<s>"
|
74 |
+
for user_prompt, bot_response in history:
|
75 |
+
formatted_prompt += f"[INST] {user_prompt} [/INST]"
|
76 |
+
formatted_prompt += f" {bot_response}</s> "
|
77 |
+
formatted_prompt += f"[INST] {prompt} [/INST]"
|
78 |
+
|
79 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
80 |
+
output = ""
|
81 |
+
|
82 |
+
for response in stream:
|
83 |
+
output += response.token.text
|
84 |
+
yield output
|
85 |
+
return output
|
86 |
+
|
87 |
+
# Define additional input components for the Gradio interface
|
88 |
+
additional_inputs = [
|
89 |
+
gr.Slider(
|
90 |
+
label="Temperature",
|
91 |
+
value=0.7,
|
92 |
+
minimum=0.0,
|
93 |
+
maximum=1.0,
|
94 |
+
step=0.05,
|
95 |
+
interactive=True,
|
96 |
+
info="Higher values produce more diverse outputs",
|
97 |
+
),
|
98 |
+
gr.Slider(
|
99 |
+
label="Max new tokens",
|
100 |
+
value=256,
|
101 |
+
minimum=0,
|
102 |
+
maximum=1024,
|
103 |
+
step=64,
|
104 |
+
interactive=True,
|
105 |
+
info="The maximum number of new tokens",
|
106 |
+
),
|
107 |
+
gr.Slider(
|
108 |
+
label="Top-p (nucleus sampling)",
|
109 |
+
value=0.95,
|
110 |
+
minimum=0.0,
|
111 |
+
maximum=1,
|
112 |
+
step=0.05,
|
113 |
+
interactive=True,
|
114 |
+
info="Higher values sample more low-probability tokens",
|
115 |
+
),
|
116 |
+
gr.Slider(
|
117 |
+
label="Repetition penalty",
|
118 |
+
value=1.1,
|
119 |
+
minimum=1.0,
|
120 |
+
maximum=2.0,
|
121 |
+
step=0.05,
|
122 |
+
interactive=True,
|
123 |
+
info="Penalize repeated tokens",
|
124 |
+
)
|
125 |
+
]
|
126 |
+
|
127 |
+
# Define CSS for styling the Gradio interface
|
128 |
+
css = """
|
129 |
+
#mkd {
|
130 |
+
height: 500px;
|
131 |
+
overflow: auto;
|
132 |
+
border: 1px solid #ccc;
|
133 |
+
}
|
134 |
+
"""
|
135 |
+
|
136 |
+
# Create the Gradio interface with the chat component
|
137 |
+
with gr.Blocks(css=css) as demo:
|
138 |
+
gr.HTML("<h1><center>Mistral 7B Instruct<h1><center>")
|
139 |
+
gr.HTML("<h3><center>In this demo, you can chat with <a href='https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1'>Mistral-7B-Instruct</a> model. 📜<h3><center>")
|
140 |
+
gr.HTML("<h3><center>Learn more about the model <a href='https://huggingface.co/docs/transformers/main/model_doc/mistral'>here</a>. 📚<h3><center>")
|
141 |
+
gr.ChatInterface(
|
142 |
+
generate,
|
143 |
+
additional_inputs=additional_inputs,
|
144 |
+
examples=[["What is the secret to life?"], ["Write me a recipe for pancakes."]],
|
145 |
+
)
|
146 |
+
|
147 |
+
# Launch the Gradio interface with debugging enabled
|
148 |
+
demo.queue().launch(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==2.1.4
|
2 |
+
requests==2.26.0
|
3 |
+
beautifulsoup4==4.10.0
|
4 |
+
huggingface-hub==0.0.17
|
5 |
+
nest-asyncio==1.5.1
|
6 |
+
sentence-transformers==2.1.0
|
7 |
+
torch==1.9.0
|
8 |
+
transformers==4.11.3
|
9 |
+
langchain==0.6.2
|