Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -78,31 +78,34 @@ def update_vectors(files, parser):
|
|
78 |
|
79 |
return f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files using {parser}."
|
80 |
|
81 |
-
def generate_chunked_response(prompt, model, max_tokens=1000,
|
82 |
client = InferenceClient(
|
83 |
model,
|
84 |
token=huggingface_token,
|
85 |
)
|
86 |
|
87 |
-
|
88 |
messages = [{"role": "user", "content": prompt}]
|
89 |
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
|
|
103 |
|
104 |
-
#
|
105 |
-
|
|
|
106 |
clean_response = clean_response.replace("Using the following context:", "").strip()
|
107 |
clean_response = clean_response.replace("Using the following context from the PDF documents:", "").strip()
|
108 |
|
|
|
78 |
|
79 |
return f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files using {parser}."
|
80 |
|
81 |
+
def generate_chunked_response(prompt, model, max_tokens=1000, num_calls=3, temperature=0.2):
|
82 |
client = InferenceClient(
|
83 |
model,
|
84 |
token=huggingface_token,
|
85 |
)
|
86 |
|
87 |
+
full_responses = []
|
88 |
messages = [{"role": "user", "content": prompt}]
|
89 |
|
90 |
+
for _ in range(num_calls):
|
91 |
+
try:
|
92 |
+
response = ""
|
93 |
+
for message in client.chat_completion(
|
94 |
+
messages=messages,
|
95 |
+
max_tokens=max_tokens,
|
96 |
+
temperature=temperature,
|
97 |
+
stream=True,
|
98 |
+
):
|
99 |
+
if message.choices and message.choices[0].delta and message.choices[0].delta.content:
|
100 |
+
chunk = message.choices[0].delta.content
|
101 |
+
response += chunk
|
102 |
+
full_responses.append(response)
|
103 |
+
except Exception as e:
|
104 |
+
print(f"Error in generating response: {str(e)}")
|
105 |
|
106 |
+
# Combine and clean up the responses
|
107 |
+
combined_response = " ".join(full_responses)
|
108 |
+
clean_response = re.sub(r'<s>\[INST\].*?\[/INST\]\s*', '', combined_response, flags=re.DOTALL)
|
109 |
clean_response = clean_response.replace("Using the following context:", "").strip()
|
110 |
clean_response = clean_response.replace("Using the following context from the PDF documents:", "").strip()
|
111 |
|