fixed imports
Browse files- app.py +66 -59
- requirements.txt +8 -1
app.py
CHANGED
@@ -1,63 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
""
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
-
|
28 |
-
response = ""
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
"""
|
43 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
44 |
-
"""
|
45 |
-
demo = gr.ChatInterface(
|
46 |
-
respond,
|
47 |
-
additional_inputs=[
|
48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
51 |
-
gr.Slider(
|
52 |
-
minimum=0.1,
|
53 |
-
maximum=1.0,
|
54 |
-
value=0.95,
|
55 |
-
step=0.05,
|
56 |
-
label="Top-p (nucleus sampling)",
|
57 |
-
),
|
58 |
-
],
|
59 |
)
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
-
|
63 |
-
|
|
|
1 |
+
import os
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
4 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
5 |
+
from langchain_huggingface import HuggingFacePipeline
|
6 |
+
from langchain_community.vectorstores import FAISS
|
7 |
+
from langchain.chains import RetrievalQA
|
8 |
import gradio as gr
|
9 |
+
import spaces
|
10 |
+
|
11 |
+
|
12 |
+
# Load TinyLlama model
|
13 |
+
model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
15 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16)
|
16 |
+
|
17 |
+
# Create a text generation pipeline
|
18 |
+
pipe = pipeline(
|
19 |
+
"text-generation",
|
20 |
+
model=model,
|
21 |
+
tokenizer=tokenizer,
|
22 |
+
max_new_tokens=512,
|
23 |
+
do_sample=True,
|
24 |
+
temperature=0.7,
|
25 |
+
top_p=0.95,
|
26 |
+
top_k=40,
|
27 |
+
repetition_penalty=1.1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
)
|
29 |
|
30 |
+
# Wrap the pipeline in a LangChain HuggingFacePipeline
|
31 |
+
llm = HuggingFacePipeline(pipeline=pipe)
|
32 |
+
|
33 |
+
# Load embeddings
|
34 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
35 |
+
|
36 |
+
# Load the FAISS index
|
37 |
+
db_FAISS = FAISS.load_local("/home/user/app/", embeddings, allow_dangerous_deserialization=True)
|
38 |
+
|
39 |
+
# Create a RetrievalQA chain
|
40 |
+
qa_chain = RetrievalQA.from_chain_type(
|
41 |
+
llm=llm,
|
42 |
+
chain_type="stuff",
|
43 |
+
retriever=db_FAISS.as_retriever(search_kwargs={"k": 3}),
|
44 |
+
return_source_documents=True
|
45 |
+
)
|
46 |
+
|
47 |
+
print("fuck14")
|
48 |
+
@spaces.GPU
|
49 |
+
def query_documents(query):
|
50 |
+
result = qa_chain({"query": query})
|
51 |
+
answer = result['result']
|
52 |
+
sources = [doc.metadata for doc in result['source_documents']]
|
53 |
+
return answer, sources
|
54 |
+
|
55 |
+
# Gradio interface
|
56 |
+
def gradio_interface(query):
|
57 |
+
answer, sources = query_documents(query)
|
58 |
+
source_text = "\n\nSources:\n" + "\n".join([f"Source: {s.get('source', 'Unknown')}, Page: {s.get('page', 'Unknown')}" for s in sources])
|
59 |
+
return answer + source_text
|
60 |
+
|
61 |
+
iface = gr.Interface(
|
62 |
+
fn=gradio_interface,
|
63 |
+
inputs="text",
|
64 |
+
outputs="text",
|
65 |
+
title="Document Q&A with TinyLlama",
|
66 |
+
description="Ask questions about your documents"
|
67 |
+
)
|
68 |
|
69 |
+
# Hugging Face Spaces
|
70 |
+
iface.launch()
|
requirements.txt
CHANGED
@@ -1 +1,8 @@
|
|
1 |
-
huggingface_hub
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
huggingface_hub
|
2 |
+
langchain
|
3 |
+
sentence-transformers
|
4 |
+
langchain-community
|
5 |
+
transformers
|
6 |
+
torch
|
7 |
+
faiss-gpu
|
8 |
+
langchain_huggingface
|