makhdoomnaeem commited on
Commit
337ef1c
·
verified ·
1 Parent(s): e6dcc7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -22
app.py CHANGED
@@ -61,20 +61,7 @@ except Exception as e:
61
  st.error(f"Details: {str(e)}")
62
 
63
  # Helper Functions
64
- def add_human_noise(output_text):
65
- noise_phrases = [
66
- "you know,", "like I said,", "to be honest,", "frankly,",
67
- "truth be told,", "I mean,", "well, honestly,", "if I'm being real,"
68
- ]
69
- filler_words = ["uh,", "um,", "so,", "well,", "I guess,"]
70
- sentences = output_text.split(". ")
71
- noisy_sentences = [
72
- f"{random.choice(noise_phrases)} {sentence}" if random.random() < 0.2 else sentence
73
- for sentence in sentences
74
- ]
75
- return ". ".join(noisy_sentences)
76
-
77
- def adjust_sentence_structure(output_text):
78
  sentences = output_text.split(". ")
79
  adjusted_sentences = []
80
 
@@ -82,24 +69,24 @@ def adjust_sentence_structure(output_text):
82
  words = sentence.split()
83
  if len(words) > 15:
84
  split_point = random.randint(len(words) // 3, 2 * len(words) // 3)
85
- adjusted_sentences.append(" ".join(words[:split_point]) + ", you know,")
86
  adjusted_sentences.append(" ".join(words[split_point:]))
87
  else:
88
  adjusted_sentences.append(sentence)
89
-
90
  return ". ".join(adjusted_sentences)
91
 
92
  def check_grammar(output_text):
93
  if tool is None:
94
- return output_text # Skip grammar check if LanguageTool isn't initialized
95
  matches = tool.check(output_text)
96
  corrected_text = tool.correct(output_text)
97
  return corrected_text
98
 
99
  def refine_humanization(output_text, tone, iteration):
100
  refinement_prompt = (
101
- f"Refine the following text to sound natural, slightly imperfect, and conversational. "
102
- f"Ensure it aligns with a {tone} tone: {output_text}"
103
  )
104
  try:
105
  refinement_response = client.chat.completions.create(
@@ -109,13 +96,13 @@ def refine_humanization(output_text, tone, iteration):
109
  )
110
  refined_text = refinement_response.choices[0].message.content.strip()
111
 
 
112
  if iteration > 1:
113
- refined_text = add_human_noise(refined_text)
114
- refined_text = adjust_sentence_structure(refined_text)
115
 
116
  return check_grammar(refined_text)
117
  except Exception:
118
- return output_text # Return original output if refinement fails
119
 
120
  def split_text_into_chunks(text, max_words=500):
121
  words = text.split()
 
61
  st.error(f"Details: {str(e)}")
62
 
63
  # Helper Functions
64
+ def adjust_sentence_structure(output_text, tone):
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  sentences = output_text.split(". ")
66
  adjusted_sentences = []
67
 
 
69
  words = sentence.split()
70
  if len(words) > 15:
71
  split_point = random.randint(len(words) // 3, 2 * len(words) // 3)
72
+ adjusted_sentences.append(" ".join(words[:split_point]))
73
  adjusted_sentences.append(" ".join(words[split_point:]))
74
  else:
75
  adjusted_sentences.append(sentence)
76
+
77
  return ". ".join(adjusted_sentences)
78
 
79
  def check_grammar(output_text):
80
  if tool is None:
81
+ return output_text
82
  matches = tool.check(output_text)
83
  corrected_text = tool.correct(output_text)
84
  return corrected_text
85
 
86
  def refine_humanization(output_text, tone, iteration):
87
  refinement_prompt = (
88
+ f"Refine the following text to sound professional and natural, avoiding repetitive patterns. "
89
+ f"Ensure it matches a {tone} tone. Here's the text: {output_text}"
90
  )
91
  try:
92
  refinement_response = client.chat.completions.create(
 
96
  )
97
  refined_text = refinement_response.choices[0].message.content.strip()
98
 
99
+ # Apply structure adjustments in later iterations
100
  if iteration > 1:
101
+ refined_text = adjust_sentence_structure(refined_text, tone)
 
102
 
103
  return check_grammar(refined_text)
104
  except Exception:
105
+ return output_text
106
 
107
  def split_text_into_chunks(text, max_words=500):
108
  words = text.split()