fehmikaya commited on
Commit
a86a7c9
·
verified ·
1 Parent(s): cd8b476

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -6
app.py CHANGED
@@ -1,9 +1,50 @@
1
  import streamlit as st
2
 
3
  import time
 
 
 
 
 
 
4
 
5
  icons = {"assistant": "robot.png", "user": "man-kddi.png"}
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  def streamer(text):
8
  for i in text:
9
  yield i
@@ -26,7 +67,7 @@ for message in st.session_state.messages:
26
 
27
  with st.sidebar:
28
  uploaded_file = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button")
29
- video_url = st.text_input("Web Link: ")
30
  if st.button("Submit & Process"):
31
  with st.spinner("Processing..."):
32
  print("Processing files")
@@ -34,23 +75,23 @@ with st.sidebar:
34
  if uploaded_file:
35
  print("File uploaded")
36
 
37
- if video_url:
38
- print("Link uploaded:"+video_url)
39
 
40
  st.success("Done")
41
  st.text_area("Console", st.session_state["console_out"])
42
 
43
  user_prompt = st.chat_input("Ask me anything about the content of the PDF or Web Link:")
44
 
45
- if user_prompt and (uploaded_file or video_url):
46
  st.session_state.messages.append({'role': 'user', "content": user_prompt})
47
  with st.chat_message("user", avatar="man-kddi.png"):
48
  st.write(user_prompt)
49
 
50
  # Trigger assistant's response retrieval and update UI
51
  with st.spinner("Thinking..."):
52
- response = "I have an answer coming soon..."
53
- st.session_state["console_out"] += response + "\n"
54
  with st.chat_message("user", avatar="robot.png"):
55
  st.write_stream(streamer(response))
56
  st.session_state.messages.append({'role': 'assistant', "content": response})
 
1
  import streamlit as st
2
 
3
  import time
4
+ import os
5
+
6
+ from customllama3 import CustomLlama3
7
+
8
+ from langchain_core.output_parsers import JsonOutputParser
9
+ from langchain_core.prompts import PromptTemplate
10
 
11
  icons = {"assistant": "robot.png", "user": "man-kddi.png"}
12
 
13
+ # Get the API key from the environment variable
14
+ HF_TOKEN = os.getenv("HF_TOKEN")
15
+ if HF_TOKEN is None:
16
+ st.error("API key not found. Please set the HF_TOKEN secret in your Hugging Face Space.")
17
+ st.stop()
18
+
19
+ remote_llm = CustomLlama3(bearer_token = HF_TOKEN)
20
+
21
+ def retrieval_grader(question):
22
+ prompt = PromptTemplate(
23
+ template="""<|begin_of_text|><|start_header_id|>system<|end_header_id|> You are a grader assessing relevance
24
+ of a retrieved document to a user question. If the document contains keywords related to the user question,
25
+ grade it as relevant. It does not need to be a stringent test. The goal is to filter out erroneous retrievals. \n
26
+ Give a binary score 'yes' or 'no' score to indicate whether the document is relevant to the question. \n
27
+ Provide the binary score as a JSON with a single key 'score' and no premable or explanation.
28
+ <|eot_id|><|start_header_id|>user<|end_header_id|>
29
+ Here is the retrieved document: \n\n {document} \n\n
30
+ Here is the user question: {question} \n <|eot_id|><|start_header_id|>assistant<|end_header_id|>
31
+ """,
32
+ input_variables=["question", "document"]
33
+ )
34
+
35
+ retrieval_grader = prompt | remote_llm | JsonOutputParser()
36
+
37
+ # Example usage
38
+ document = "Apples are rich in vitamins and fiber."
39
+
40
+ result = retrieval_grader.invoke({
41
+ "question": question,
42
+ "document": document
43
+ })
44
+
45
+ return result["score"]
46
+
47
+
48
  def streamer(text):
49
  for i in text:
50
  yield i
 
67
 
68
  with st.sidebar:
69
  uploaded_file = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button")
70
+ web_url = st.text_input("Web Link: ")
71
  if st.button("Submit & Process"):
72
  with st.spinner("Processing..."):
73
  print("Processing files")
 
75
  if uploaded_file:
76
  print("File uploaded")
77
 
78
+ if web_url:
79
+ print("Link uploaded:"+web_url)
80
 
81
  st.success("Done")
82
  st.text_area("Console", st.session_state["console_out"])
83
 
84
  user_prompt = st.chat_input("Ask me anything about the content of the PDF or Web Link:")
85
 
86
+ if user_prompt: # and (uploaded_file or video_url)
87
  st.session_state.messages.append({'role': 'user', "content": user_prompt})
88
  with st.chat_message("user", avatar="man-kddi.png"):
89
  st.write(user_prompt)
90
 
91
  # Trigger assistant's response retrieval and update UI
92
  with st.spinner("Thinking..."):
93
+ response = retrieval_grader(user_prompt)
94
+ st.session_state["console_out"] += "retrieval_grader" + user_prompt + "\n"
95
  with st.chat_message("user", avatar="robot.png"):
96
  st.write_stream(streamer(response))
97
  st.session_state.messages.append({'role': 'assistant', "content": response})