bubuuunel commited on
Commit
4bcea7d
1 Parent(s): 7ea5134

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -44
app.py CHANGED
@@ -2,7 +2,6 @@
2
 
3
  ## Setup
4
  # Import the necessary Libraries
5
-
6
  import json
7
  import gradio as gr
8
  import uuid
@@ -20,8 +19,6 @@ from google.colab import userdata, drive
20
  from huggingface_hub import CommitScheduler
21
 
22
 
23
-
24
-
25
  # Create Client
26
  load_dotenv()
27
 
@@ -34,9 +31,7 @@ client = OpenAI(
34
 
35
  # Define the embedding model and the vectorstore
36
  embedding_model = SentenceTransformerEmbeddings(model_name='thenlper/gte-large')
37
-
38
  # Load the persisted vectorDB
39
-
40
  reportdb = Chroma(
41
  collection_name=collection_name,
42
  persist_directory='./report_db1',
@@ -49,7 +44,7 @@ log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
49
  log_folder = log_file.parent
50
 
51
  scheduler = CommitScheduler(
52
- repo_id="report-logs",
53
  repo_type="dataset",
54
  folder_path=log_folder,
55
  path_in_repo="data",
@@ -74,6 +69,7 @@ If the answer is not found in the context, respond "I don't know".
74
 
75
 
76
  # Define the user message template
 
77
  qna_user_message_template = """
78
  ###Context
79
  Here are some documents that are relevant to the question mentioned below.
@@ -84,7 +80,6 @@ Here are some documents that are relevant to the question mentioned below.
84
  """
85
 
86
 
87
-
88
  # Define the predict function that runs when 'Submit' is clicked or when a API request is made
89
  def predict(user_input,company):
90
 
@@ -92,51 +87,47 @@ def predict(user_input,company):
92
  relevant_document_chunks = vectorstore_persisted.similarity_search(user_input, k=5, filter={"source":filter})
93
 
94
  # Create context_for_query
95
-
96
- relevant_document_chunks = retriever.get_relevant_documents(user_question)
97
- context_list = [d.page_content for d in relevant_document_chunks]
98
- context_for_query = ". ".join(context_list)
99
-
100
- prompt = [
101
- {'role':'system', 'content': qna_system_message},
102
- {'role': 'user', 'content': qna_user_message_template.format(
103
- context=context_for_query,
104
- question=user_question
105
- )
106
- }
107
- ]
108
-
109
-
110
-
111
- print(prompt)
112
-
113
  # Create messages
114
- response = client.chat.completions.create(
115
- model=model_name,
116
- messages=prompt,
117
- temperature=0
118
- )
 
119
 
120
  # Get response from the LLM
121
- answer = response.choices[0].message.content.strip()
122
- print (answer)
123
 
124
  # While the prediction is made, log both the inputs and outputs to a local log file
125
  # While writing to the log file, ensure that the commit scheduler is locked to avoid parallel
126
  # access
127
 
128
- with scheduler.lock:
129
- with log_file.open("a") as f:
130
- f.write(json.dumps(
131
- {
132
- 'user_input': user_input,
133
- 'retrieved_context': context_for_query,
134
- 'model_response': prediction
135
- }
136
- ))
137
- f.write("\n")
138
 
139
- return prediction
140
 
141
  # Set-up the Gradio UI
142
  # Add text box and radio button to the interface
@@ -148,7 +139,6 @@ company = gr.Radio()
148
  # Create the interface
149
  # For the inputs parameter of Interface provide [textbox,company]
150
 
151
- demo = gr.Interface(inputs=[textbox,company], fn = predict, output ='text')
152
 
153
  demo.queue()
154
  demo.launch()
 
2
 
3
  ## Setup
4
  # Import the necessary Libraries
 
5
  import json
6
  import gradio as gr
7
  import uuid
 
19
  from huggingface_hub import CommitScheduler
20
 
21
 
 
 
22
  # Create Client
23
  load_dotenv()
24
 
 
31
 
32
  # Define the embedding model and the vectorstore
33
  embedding_model = SentenceTransformerEmbeddings(model_name='thenlper/gte-large')
 
34
  # Load the persisted vectorDB
 
35
  reportdb = Chroma(
36
  collection_name=collection_name,
37
  persist_directory='./report_db1',
 
44
  log_folder = log_file.parent
45
 
46
  scheduler = CommitScheduler(
47
+ repo_id="---------",
48
  repo_type="dataset",
49
  folder_path=log_folder,
50
  path_in_repo="data",
 
69
 
70
 
71
  # Define the user message template
72
+
73
  qna_user_message_template = """
74
  ###Context
75
  Here are some documents that are relevant to the question mentioned below.
 
80
  """
81
 
82
 
 
83
  # Define the predict function that runs when 'Submit' is clicked or when a API request is made
84
  def predict(user_input,company):
85
 
 
87
  relevant_document_chunks = vectorstore_persisted.similarity_search(user_input, k=5, filter={"source":filter})
88
 
89
  # Create context_for_query
90
+ relevant_document_chunks = retriever.get_relevant_documents(user_question)
91
+ context_list = [d.page_content for d in relevant_document_chunks]
92
+ context_for_query = ". ".join(context_list)
93
+
94
+ prompt = [
95
+ {'role':'system', 'content': qna_system_message},
96
+ {'role': 'user', 'content': qna_user_message_template.format(
97
+ context=context_for_query,
98
+ question=user_question
99
+ )
100
+ }
101
+ ]
102
+ print(prompt)
 
 
 
 
 
103
  # Create messages
104
+ response = client.chat.completions.create(
105
+ model=model_name,
106
+ messages=prompt,
107
+ temperature=0
108
+ )
109
+
110
 
111
  # Get response from the LLM
112
+ answer = response.choices[0].message.content.strip()
113
+ print (answer)
114
 
115
  # While the prediction is made, log both the inputs and outputs to a local log file
116
  # While writing to the log file, ensure that the commit scheduler is locked to avoid parallel
117
  # access
118
 
119
+ with scheduler.lock:
120
+ with log_file.open("a") as f:
121
+ f.write(json.dumps(
122
+ {
123
+ 'user_input': user_input,
124
+ 'retrieved_context': context_for_query,
125
+ 'model_response': prediction
126
+ }
127
+ ))
128
+ f.write("\n")
129
 
130
+ return prediction
131
 
132
  # Set-up the Gradio UI
133
  # Add text box and radio button to the interface
 
139
  # Create the interface
140
  # For the inputs parameter of Interface provide [textbox,company]
141
 
 
142
 
143
  demo.queue()
144
  demo.launch()