Shreyas094 commited on
Commit
b9b22f5
·
verified ·
1 Parent(s): 78d4f2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -17
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, max_chunks=5, temperature=0.2):
82
  client = InferenceClient(
83
  model,
84
  token=huggingface_token,
85
  )
86
 
87
- full_response = ""
88
  messages = [{"role": "user", "content": prompt}]
89
 
90
- try:
91
- for message in client.chat_completion(
92
- messages=messages,
93
- max_tokens=max_tokens,
94
- temperature=temperature,
95
- stream=True,
96
- ):
97
- chunk = message.choices[0].delta.content
98
- if chunk:
99
- full_response += chunk
100
-
101
- except Exception as e:
102
- print(f"Error in generating response: {str(e)}")
 
 
103
 
104
- # Clean up the response
105
- clean_response = re.sub(r'<s>\[INST\].*?\[/INST\]\s*', '', full_response, flags=re.DOTALL)
 
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