Shreyas094 commited on
Commit
711bfec
1 Parent(s): d1372f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -4
app.py CHANGED
@@ -79,7 +79,8 @@ def generate_chunked_response(prompt, max_tokens=1000, max_chunks=5):
79
  "temperature": 0.7,
80
  "top_p": 0.95,
81
  "top_k": 40,
82
- "repetition_penalty": 1.1
 
83
  }
84
  }
85
 
@@ -90,14 +91,31 @@ def generate_chunked_response(prompt, max_tokens=1000, max_chunks=5):
90
  result = response.json()
91
  if isinstance(result, list) and len(result) > 0:
92
  chunk = result[0].get('generated_text', '')
93
- full_response += chunk
94
- if chunk.endswith((".", "!", "?")):
 
 
 
 
 
 
 
 
95
  break
 
 
 
96
  else:
97
  break
98
  else:
99
  break
100
- return full_response.strip()
 
 
 
 
 
 
101
 
102
  def duckduckgo_search(query):
103
  with DDGS() as ddgs:
 
79
  "temperature": 0.7,
80
  "top_p": 0.95,
81
  "top_k": 40,
82
+ "repetition_penalty": 1.1,
83
+ "stop": ["</s>", "[/INST]"] # Add stop tokens
84
  }
85
  }
86
 
 
91
  result = response.json()
92
  if isinstance(result, list) and len(result) > 0:
93
  chunk = result[0].get('generated_text', '')
94
+
95
+ # Remove any part of the chunk that's already in full_response
96
+ new_content = chunk[len(full_response):].strip()
97
+
98
+ if not new_content:
99
+ break # No new content, so we're done
100
+
101
+ full_response += new_content
102
+
103
+ if chunk.endswith((".", "!", "?", "</s>", "[/INST]")):
104
  break
105
+
106
+ # Update the prompt for the next iteration
107
+ payload["inputs"] = full_response
108
  else:
109
  break
110
  else:
111
  break
112
+
113
+ # Clean up the response
114
+ clean_response = re.sub(r'<s>\[INST\].*?\[/INST\]\s*', '', full_response, flags=re.DOTALL)
115
+ clean_response = clean_response.replace("Using the following context:", "").strip()
116
+ clean_response = clean_response.replace("Using the following context from the PDF documents:", "").strip()
117
+
118
+ return clean_response
119
 
120
  def duckduckgo_search(query):
121
  with DDGS() as ddgs: