sanuth123 commited on
Commit
ee0fd46
·
verified ·
1 Parent(s): 0bbfaaa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -31
app.py CHANGED
@@ -1,52 +1,67 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
 
 
 
 
 
 
 
 
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
 
10
  def respond(
11
  message,
12
- history: list[tuple[str, str]],
13
  system_message,
14
  max_tokens,
15
  temperature,
16
  top_p,
17
  ):
18
- messages = [{"role": "system", "content": system_message}]
 
19
 
20
- for val in history:
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
- """
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=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
  gr.Slider(
@@ -59,6 +74,5 @@ demo = gr.ChatInterface(
59
  ],
60
  )
61
 
62
-
63
  if __name__ == "__main__":
64
  demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ from langchain.embeddings import HuggingFaceEmbeddings
4
+ from langchain.vectorstores import FAISS
5
+ from langchain_community.document_loaders import PyPDFLoader
6
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
7
+ from langchain import hub
8
+ from langchain_core.output_parsers import StrOutputParser
9
+ from langchain_core.runnables import RunnablePassthrough
10
+ from langchain.chains.combine_documents import create_stuff_documents_chain
11
+ from langchain_core.prompts import ChatPromptTemplate
12
+ from langchain.chains import create_retrieval_chain
13
 
14
+ # Set up Inference Client
 
 
15
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
16
 
17
+ # Load the vector database
18
+ vectorstore = FAISS.load_local("vectorstore.db")
19
+ retriever = vectorstore.as_retriever()
20
+
21
+ # Define LLM configuration
22
+ llm = client # Assuming the model on Hugging Face is used directly via API
23
+ prompt_template = """
24
+ You are an assistant for question-answering tasks.
25
+ Answer the given questions.
26
+
27
+ <context>
28
+ {context}
29
+ </context>
30
+
31
+ Question: {input}
32
+ """
33
+
34
+ # Function to create the prompt template
35
+ prompt = ChatPromptTemplate.from_template(prompt_template)
36
+ doc_chain = create_stuff_documents_chain(llm, prompt)
37
+ chain = create_retrieval_chain(retriever, doc_chain)
38
 
39
+ # Chatbot response function
40
  def respond(
41
  message,
42
+ history,
43
  system_message,
44
  max_tokens,
45
  temperature,
46
  top_p,
47
  ):
48
+ # Use the RAG model to retrieve relevant context and answer the question
49
+ response = chain.invoke({"input": message})
50
 
51
+ # Extract the answer from the response
52
+ answer = response.get('answer', "Sorry, I couldn't find an answer.")
53
+
54
+ # Add conversation to history
55
+ history.append((message, answer))
56
 
57
+ # Return the updated history
58
+ return "", history
59
 
60
+ # Gradio Chat Interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  demo = gr.ChatInterface(
62
  respond,
63
  additional_inputs=[
64
+ gr.Textbox(value="You are a helpful assistant.", label="System message"),
65
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
66
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
67
  gr.Slider(
 
74
  ],
75
  )
76
 
 
77
  if __name__ == "__main__":
78
  demo.launch()