Spaces:
Sleeping
Sleeping
ashishanand
commited on
Commit
·
4a9200b
1
Parent(s):
026406c
changed prompt
Browse files
app.py
CHANGED
@@ -19,50 +19,81 @@ chat_client = Groq(api_key=groq_api_key)
|
|
19 |
model = "llama-3.2-90b-text-preview"
|
20 |
|
21 |
def edit_text(text):
|
22 |
-
|
23 |
-
citation_matches = list(re.finditer(r'\[(\d+)\]', text))
|
24 |
-
|
25 |
-
# List to store indices of citations to remove
|
26 |
-
indices_to_remove = []
|
27 |
-
|
28 |
prev_num = None
|
29 |
-
|
|
|
30 |
|
31 |
-
#
|
32 |
-
for
|
33 |
-
|
34 |
-
current_num =
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
indices_to_remove.append(prev_index)
|
39 |
-
prev_num = current_num
|
40 |
-
prev_index = i
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
for i in range(len(citation_matches)):
|
47 |
-
m = citation_matches[i]
|
48 |
-
start, end = m.span()
|
49 |
-
if i in indices_to_remove:
|
50 |
-
# Remove citation
|
51 |
-
output_parts.append(text[last_end:start])
|
52 |
else:
|
53 |
-
#
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
output_parts.append(new_citation)
|
58 |
last_end = end
|
59 |
|
60 |
# Append any remaining text after the last citation
|
61 |
-
|
62 |
|
63 |
-
|
|
|
64 |
return modified_text
|
65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
# def parse_pdf(pdf_path):
|
67 |
|
68 |
# texts = []
|
@@ -83,30 +114,52 @@ def edit_text(text):
|
|
83 |
# text = re.sub(r'\s+', ' ', text)
|
84 |
# text = text.strip()
|
85 |
# return text
|
86 |
-
|
87 |
def call_Llama_api(query, context):
|
88 |
-
# ... (same as your original function)
|
89 |
chat_completion = chat_client.chat.completions.create(
|
90 |
-
messages=[
|
91 |
{
|
92 |
"role": "system",
|
93 |
-
"content": "You are a car technician. Given the user's question and relevant excerpts from different car manuals, answer the question by including direct quotes from the
|
94 |
},
|
95 |
{
|
96 |
"role": "user",
|
97 |
"content": "User Question: " + query + "\n\nRelevant Excerpt(s):\n\n" + context,
|
98 |
}
|
99 |
],
|
100 |
-
|
101 |
max_tokens=200,
|
102 |
top_p=1,
|
103 |
stream=False,
|
104 |
-
stop=None,
|
105 |
-
model=model
|
106 |
)
|
|
|
107 |
response = chat_completion.choices[0].message.content
|
108 |
return response
|
109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
# def chunk_texts(texts, max_tokens=500, overlap_tokens=50):
|
112 |
# """
|
|
|
19 |
model = "llama-3.2-90b-text-preview"
|
20 |
|
21 |
def edit_text(text):
|
22 |
+
pattern = re.compile(r'\[(\d+)\]')
|
|
|
|
|
|
|
|
|
|
|
23 |
prev_num = None
|
24 |
+
output = []
|
25 |
+
last_end = 0
|
26 |
|
27 |
+
# Iterate over the citations using finditer
|
28 |
+
for match in pattern.finditer(text):
|
29 |
+
start, end = match.span()
|
30 |
+
current_num = match.group(1)
|
31 |
|
32 |
+
# Append text from the end of the last match to the start of the current match
|
33 |
+
output.append(text[last_end:start])
|
|
|
|
|
|
|
34 |
|
35 |
+
if prev_num == current_num:
|
36 |
+
# Remove the previous citation by not appending it
|
37 |
+
pass # We effectively skip appending anything, which removes the duplicate
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
else:
|
39 |
+
# Append the current citation with 'Page ' added
|
40 |
+
output.append(f'[Page {current_num}]')
|
41 |
+
|
42 |
+
prev_num = current_num
|
|
|
43 |
last_end = end
|
44 |
|
45 |
# Append any remaining text after the last citation
|
46 |
+
output.append(text[last_end:])
|
47 |
|
48 |
+
# Join all parts to form the modified text
|
49 |
+
modified_text = ''.join(output)
|
50 |
return modified_text
|
51 |
|
52 |
+
# def edit_text(text):
|
53 |
+
# # Find all citations and their positions
|
54 |
+
# citation_matches = list(re.finditer(r'\[(\d+)\]', text))
|
55 |
+
|
56 |
+
# # List to store indices of citations to remove
|
57 |
+
# indices_to_remove = []
|
58 |
+
|
59 |
+
# prev_num = None
|
60 |
+
# prev_index = None
|
61 |
+
|
62 |
+
# # Identify consecutive duplicate citations
|
63 |
+
# for i in range(len(citation_matches)):
|
64 |
+
# current_citation = citation_matches[i]
|
65 |
+
# current_num = current_citation.group(1)
|
66 |
+
|
67 |
+
# if prev_num == current_num:
|
68 |
+
# # Mark the previous citation for removal
|
69 |
+
# indices_to_remove.append(prev_index)
|
70 |
+
# prev_num = current_num
|
71 |
+
# prev_index = i
|
72 |
+
|
73 |
+
# # Reconstruct the text with modifications
|
74 |
+
# output_parts = []
|
75 |
+
# last_end = 0
|
76 |
+
|
77 |
+
# for i in range(len(citation_matches)):
|
78 |
+
# m = citation_matches[i]
|
79 |
+
# start, end = m.span()
|
80 |
+
# if i in indices_to_remove:
|
81 |
+
# # Remove citation
|
82 |
+
# output_parts.append(text[last_end:start])
|
83 |
+
# else:
|
84 |
+
# # Keep and modify citation
|
85 |
+
# output_parts.append(text[last_end:start])
|
86 |
+
# page_num = m.group(1)
|
87 |
+
# new_citation = '[Page ' + page_num + ']'
|
88 |
+
# output_parts.append(new_citation)
|
89 |
+
# last_end = end
|
90 |
+
|
91 |
+
# # Append any remaining text after the last citation
|
92 |
+
# output_parts.append(text[last_end:])
|
93 |
+
|
94 |
+
# modified_text = ''.join(output_parts)
|
95 |
+
# return modified_text
|
96 |
+
|
97 |
# def parse_pdf(pdf_path):
|
98 |
|
99 |
# texts = []
|
|
|
114 |
# text = re.sub(r'\s+', ' ', text)
|
115 |
# text = text.strip()
|
116 |
# return text
|
|
|
117 |
def call_Llama_api(query, context):
|
|
|
118 |
chat_completion = chat_client.chat.completions.create(
|
119 |
+
messages = [
|
120 |
{
|
121 |
"role": "system",
|
122 |
+
"content": "You are a car technician. Given the user's question and relevant excerpts from different car manuals, answer the question by including direct quotes from the car manual. Always cite the page number in [] as mentioned in the excerpts used (the ones you are quoting from).\n\nFor Example: User Question: What is storage capacity of Tesla Model 3? Relevant Excerpt(s) : Page 108:: Front storage is 50 litres. , Page 150:: Rear storage is 240 litres.\n Answer: Tesla model 3 front storage is 50 litres [108] and rear storage is 240 litres [150]. Therefore total storage is 50+240 = 290 litres.\n\nHope the example above helped. Only cite an excerpt when you are explicitly referencing it."
|
123 |
},
|
124 |
{
|
125 |
"role": "user",
|
126 |
"content": "User Question: " + query + "\n\nRelevant Excerpt(s):\n\n" + context,
|
127 |
}
|
128 |
],
|
129 |
+
temperature=0.6,
|
130 |
max_tokens=200,
|
131 |
top_p=1,
|
132 |
stream=False,
|
133 |
+
stop= None,
|
134 |
+
model = model
|
135 |
)
|
136 |
+
|
137 |
response = chat_completion.choices[0].message.content
|
138 |
return response
|
139 |
|
140 |
+
# def call_Llama_api(query, context):
|
141 |
+
# # ... (same as your original function)
|
142 |
+
# chat_completion = chat_client.chat.completions.create(
|
143 |
+
# messages=[
|
144 |
+
# {
|
145 |
+
# "role": "system",
|
146 |
+
# "content": "You are a car technician. Given the user's question and relevant excerpts from different car manuals, answer the question by including direct quotes from the correct car manual. Be concise and to the point in your response."
|
147 |
+
# },
|
148 |
+
# {
|
149 |
+
# "role": "user",
|
150 |
+
# "content": "User Question: " + query + "\n\nRelevant Excerpt(s):\n\n" + context,
|
151 |
+
# }
|
152 |
+
# ],
|
153 |
+
# temperature=0.6,
|
154 |
+
# max_tokens=200,
|
155 |
+
# top_p=1,
|
156 |
+
# stream=False,
|
157 |
+
# stop=None,
|
158 |
+
# model=model
|
159 |
+
# )
|
160 |
+
# response = chat_completion.choices[0].message.content
|
161 |
+
# return response
|
162 |
+
|
163 |
|
164 |
# def chunk_texts(texts, max_tokens=500, overlap_tokens=50):
|
165 |
# """
|