Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
"""
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def respond(
|
| 11 |
message,
|
|
@@ -39,10 +115,6 @@ def respond(
|
|
| 39 |
response += token
|
| 40 |
yield response
|
| 41 |
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
additional_inputs=[
|
|
@@ -59,6 +131,5 @@ demo = gr.ChatInterface(
|
|
| 59 |
],
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
+
from langchain_ollama import OllamaLLM, OllamaEmbeddings
|
| 6 |
+
from langchain_community.vectorstores import FAISS
|
| 7 |
+
from langchain_core.prompts import PromptTemplate
|
| 8 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 9 |
+
from operator import itemgetter
|
| 10 |
import gradio as gr
|
| 11 |
+
from huggingface_hub import login, InferenceClient
|
| 12 |
|
| 13 |
+
USE_HF = True
|
| 14 |
+
MODEL_NAME = "meta-llama/Llama-2-7b-hf"
|
| 15 |
+
|
| 16 |
+
with open('mini_data.json', 'r', encoding='utf-8') as f:
|
| 17 |
+
data = json.load(f)
|
| 18 |
+
|
| 19 |
+
df = pd.DataFrame(data)
|
| 20 |
+
|
| 21 |
+
documents = [
|
| 22 |
+
f"Source: {item['Source']}\nApplication: {item['Application']}\nFunction1: {item['Function1']}\nStrategy: {item['Strategy']}"
|
| 23 |
+
for item in data
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
if USE_HF:
|
| 27 |
+
print("Using Hugging Face model...")
|
| 28 |
+
huggingface_token = os.getenv("AskNature_RAG")
|
| 29 |
+
login(token=huggingface_token)
|
| 30 |
+
# Load tokenizer and model from Hugging Face Hub
|
| 31 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, token=huggingface_token)
|
| 32 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 33 |
+
MODEL_NAME,
|
| 34 |
+
device_map="auto",
|
| 35 |
+
offload_folder="offload", # Specify the offload folder
|
| 36 |
+
token=huggingface_token
|
| 37 |
+
)
|
| 38 |
+
embeddings = OllamaEmbeddings(model=MODEL_NAME)
|
| 39 |
+
lang_model = OllamaLLM(model=MODEL_NAME)
|
| 40 |
+
else:
|
| 41 |
+
print("Using local Ollama model...")
|
| 42 |
+
MODEL = "jsk/bio-mistral"
|
| 43 |
+
embeddings = OllamaEmbeddings(model=MODEL)
|
| 44 |
+
lang_model = OllamaLLM(model=MODEL)
|
| 45 |
+
|
| 46 |
+
batch_size = 16
|
| 47 |
+
batched_embeddings = [
|
| 48 |
+
embeddings.embed_documents(documents[i:i + batch_size])
|
| 49 |
+
for i in range(0, len(documents), batch_size)
|
| 50 |
+
]
|
| 51 |
+
batched_embeddings = [embed for batch in batched_embeddings for embed in batch]
|
| 52 |
+
|
| 53 |
+
index_path = "faiss_index"
|
| 54 |
+
if os.path.exists(index_path):
|
| 55 |
+
vectorstore = FAISS.load_local(index_path, embeddings)
|
| 56 |
+
else:
|
| 57 |
+
vectorstore = FAISS.from_texts(documents, embeddings)
|
| 58 |
+
vectorstore.save_local(index_path)
|
| 59 |
+
|
| 60 |
+
retriever = vectorstore.as_retriever()
|
| 61 |
+
|
| 62 |
+
template = """
|
| 63 |
+
Answer the question based on the context below. If you can't
|
| 64 |
+
answer the question, reply "I don't know".
|
| 65 |
+
|
| 66 |
+
Context: {context}
|
| 67 |
+
|
| 68 |
+
Question: {question}
|
| 69 |
"""
|
| 70 |
+
prompt = PromptTemplate.from_template(template)
|
| 71 |
+
|
| 72 |
+
chain = {
|
| 73 |
+
"context": itemgetter("question") | retriever,
|
| 74 |
+
"question": itemgetter("question"),
|
| 75 |
+
} | prompt | lang_model | StrOutputParser()
|
| 76 |
|
| 77 |
+
def rag_qa(question):
|
| 78 |
+
try:
|
| 79 |
+
return chain.invoke({'question': question})
|
| 80 |
+
except Exception as e:
|
| 81 |
+
return f"Error: {str(e)}"
|
| 82 |
+
|
| 83 |
+
# Chatbot functionality
|
| 84 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 85 |
|
| 86 |
def respond(
|
| 87 |
message,
|
|
|
|
| 115 |
response += token
|
| 116 |
yield response
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
demo = gr.ChatInterface(
|
| 119 |
respond,
|
| 120 |
additional_inputs=[
|
|
|
|
| 131 |
],
|
| 132 |
)
|
| 133 |
|
|
|
|
| 134 |
if __name__ == "__main__":
|
| 135 |
+
demo.launch()
|