Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,24 @@
|
|
1 |
-
from
|
2 |
-
from
|
3 |
-
from
|
4 |
-
from langchain.llms import CTransformers
|
5 |
from langchain.chains import RetrievalQA
|
6 |
import gradio as gr
|
7 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
8 |
from huggingface_hub import hf_hub_download
|
9 |
-
|
10 |
-
|
11 |
|
12 |
DB_FAISS_PATH = "vectorstores/db_faiss"
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
custom_prompt_template = """Use the following pieces of information to answer the user's question.
|
15 |
If you don't know the answer, just say that you don't know, don't try to make up an answer.
|
16 |
|
@@ -25,15 +33,6 @@ def set_custom_prompt():
|
|
25 |
prompt = PromptTemplate(template=custom_prompt_template, input_variables=['context', 'question'])
|
26 |
return prompt
|
27 |
|
28 |
-
def load_llm():
|
29 |
-
llm = CTransformers(
|
30 |
-
model=model,
|
31 |
-
model_type="llama",
|
32 |
-
max_new_tokens=512,
|
33 |
-
temperature=0.5
|
34 |
-
)
|
35 |
-
return llm
|
36 |
-
|
37 |
def retrieval_QA_chain(llm, prompt, db):
|
38 |
qachain = RetrievalQA.from_chain_type(
|
39 |
llm=llm,
|
@@ -66,14 +65,14 @@ def chatbot_response(query):
|
|
66 |
except Exception as e:
|
67 |
return f"An error occurred: {str(e)}"
|
68 |
|
69 |
-
# Create a Gradio interface with updated API
|
70 |
iface = gr.Interface(
|
71 |
fn=chatbot_response,
|
72 |
inputs=gr.Textbox(lines=2, placeholder="Enter your question..."),
|
73 |
outputs="text",
|
74 |
title="Medical Chatbot",
|
75 |
-
description="Ask a medical question and get answers based on the provided context."
|
|
|
76 |
)
|
77 |
|
78 |
-
# Launch the Gradio app
|
79 |
iface.launch()
|
|
|
|
1 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
2 |
+
from langchain_community.vectorstores import FAISS
|
3 |
+
from langchain_community.llms import CTransformers
|
|
|
4 |
from langchain.chains import RetrievalQA
|
5 |
import gradio as gr
|
|
|
6 |
from huggingface_hub import hf_hub_download
|
7 |
+
import os
|
|
|
8 |
|
9 |
DB_FAISS_PATH = "vectorstores/db_faiss"
|
10 |
|
11 |
+
def load_llm():
|
12 |
+
model_name = 'TheBloke/Llama-2-7B-Chat-GGML' # Replace with the actual model repository name
|
13 |
+
model_path = hf_hub_download(repo_id=model_name, filename='pytorch_model.bin', cache_dir='./models')
|
14 |
+
llm = CTransformers(
|
15 |
+
model=model_path,
|
16 |
+
model_type="llama",
|
17 |
+
max_new_tokens=512,
|
18 |
+
temperature=0.5
|
19 |
+
)
|
20 |
+
return llm
|
21 |
+
|
22 |
custom_prompt_template = """Use the following pieces of information to answer the user's question.
|
23 |
If you don't know the answer, just say that you don't know, don't try to make up an answer.
|
24 |
|
|
|
33 |
prompt = PromptTemplate(template=custom_prompt_template, input_variables=['context', 'question'])
|
34 |
return prompt
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
def retrieval_QA_chain(llm, prompt, db):
|
37 |
qachain = RetrievalQA.from_chain_type(
|
38 |
llm=llm,
|
|
|
65 |
except Exception as e:
|
66 |
return f"An error occurred: {str(e)}"
|
67 |
|
|
|
68 |
iface = gr.Interface(
|
69 |
fn=chatbot_response,
|
70 |
inputs=gr.Textbox(lines=2, placeholder="Enter your question..."),
|
71 |
outputs="text",
|
72 |
title="Medical Chatbot",
|
73 |
+
description="Ask a medical question and get answers based on the provided context.",
|
74 |
+
live=True
|
75 |
)
|
76 |
|
|
|
77 |
iface.launch()
|
78 |
+
|