makhdoomnaeem commited on
Commit
e9c29bb
·
verified ·
1 Parent(s): 6341770

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -37
app.py CHANGED
@@ -8,26 +8,71 @@ GROQ_API_KEY = "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" # Rep
8
  os.environ["GROQ_API_KEY"] = GROQ_API_KEY
9
  client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
10
 
 
11
  def add_human_noise(output_text):
12
  """
13
  Add subtle imperfections to make the text appear more human-like.
14
  """
15
- noise_phrases = ["you know,", "honestly,", "to be fair,", "well,", "I think,"]
 
 
 
 
16
  sentences = output_text.split(". ")
17
  noisy_sentences = [
18
  f"{random.choice(noise_phrases)} {sentence}" if random.random() < 0.3 else sentence
19
  for sentence in sentences
20
  ]
 
 
 
 
21
  return ". ".join(noisy_sentences)
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  def refine_humanization(output_text, tone):
24
  """
25
  Refine text further for deeper humanization and introduce natural flow with slight noise.
26
  """
27
  refinement_prompt = (
28
- f"Take the following text and refine it to feel truly human-like, smooth, and engaging. "
29
- f"Ensure it flows naturally, uses conversational language, and introduces slight variations or imperfections. "
30
- f"The tone should be {tone}. Refine this: {output_text}"
31
  )
32
  try:
33
  refinement_response = client.chat.completions.create(
@@ -36,38 +81,12 @@ def refine_humanization(output_text, tone):
36
  stream=False,
37
  )
38
  refined_text = refinement_response.choices[0].message.content.strip()
39
- return add_human_noise(refined_text)
 
 
40
  except Exception:
41
  return output_text # Return original output if refinement fails
42
 
43
- def enforce_word_count_and_humanization(output_text, input_word_count, original_prompt, tone):
44
- """
45
- Ensure output text has a word count equal to or greater than the input word count
46
- and passes humanization refinements.
47
- """
48
- output_words = output_text.split()
49
- output_word_count = len(output_words)
50
-
51
- # If output meets word count and humanization, return as is
52
- if output_word_count >= input_word_count:
53
- return refine_humanization(output_text, tone)
54
-
55
- # If output is too short, regenerate with expansion
56
- expansion_prompt = (
57
- f"The response was too short. Expand the following text with more meaningful, detailed, and human-like content "
58
- f"that flows naturally and matches the {tone} tone: {original_prompt}"
59
- )
60
- try:
61
- expansion_response = client.chat.completions.create(
62
- messages=[{"role": "user", "content": expansion_prompt}],
63
- model="llama-3.3-70b-versatile",
64
- stream=False,
65
- )
66
- expanded_text = expansion_response.choices[0].message.content.strip()
67
- return refine_humanization(expanded_text, tone)
68
- except Exception:
69
- return output_text # Return original output in case of error
70
-
71
  def split_text_into_chunks(text, max_words=500):
72
  """
73
  Split text into chunks of a maximum number of words.
@@ -90,10 +109,17 @@ task_option = st.radio(
90
  ("Humanize Text", "Rephrase Text"),
91
  index=0
92
  )
93
-
94
- # Optional Settings
95
  tone = st.selectbox("Select tone (for humanizing):", ["Casual", "Professional", "Neutral", "Engaging", "Friendly"])
96
 
 
 
 
 
 
 
 
 
 
97
  # Generate Output
98
  if st.button("Generate Output"):
99
  if not input_text.strip():
@@ -122,13 +148,15 @@ if st.button("Generate Output"):
122
  stream=False,
123
  )
124
 
125
- # Extract and enforce word count and humanization
126
  output_text = chat_completion.choices[0].message.content.strip()
127
- output_text = enforce_word_count_and_humanization(output_text, len(chunk.split()), task_prompt, tone)
 
128
  output_chunks.append(output_text)
129
 
130
  # Combine processed chunks
131
  final_output = " ".join(output_chunks)
 
132
  output_word_count = len(final_output.split())
133
 
134
  # Display Output
 
8
  os.environ["GROQ_API_KEY"] = GROQ_API_KEY
9
  client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
10
 
11
+ # Helper Functions
12
  def add_human_noise(output_text):
13
  """
14
  Add subtle imperfections to make the text appear more human-like.
15
  """
16
+ noise_phrases = [
17
+ "you know,", "like I said,", "to be honest,", "frankly,",
18
+ "truth be told,", "I mean,", "well, honestly,", "if I'm being real,"
19
+ ]
20
+ filler_words = ["uh,", "um,", "so,", "well,", "I guess,"]
21
  sentences = output_text.split(". ")
22
  noisy_sentences = [
23
  f"{random.choice(noise_phrases)} {sentence}" if random.random() < 0.3 else sentence
24
  for sentence in sentences
25
  ]
26
+ noisy_sentences = [
27
+ f"{sentence} {random.choice(filler_words)}" if random.random() < 0.2 else sentence
28
+ for sentence in noisy_sentences
29
+ ]
30
  return ". ".join(noisy_sentences)
31
 
32
+ def adjust_sentence_structure(output_text):
33
+ """
34
+ Break down longer sentences and introduce contractions or slight redundancies
35
+ to make the text feel less polished and more conversational.
36
+ """
37
+ long_sentence_threshold = 15 # Number of words to consider a sentence "long"
38
+ sentences = output_text.split(". ")
39
+ adjusted_sentences = []
40
+
41
+ for sentence in sentences:
42
+ words = sentence.split()
43
+ if len(words) > long_sentence_threshold:
44
+ # Split long sentences into smaller ones
45
+ midpoint = len(words) // 2
46
+ adjusted_sentences.append(" ".join(words[:midpoint]) + ", you know,")
47
+ adjusted_sentences.append(" ".join(words[midpoint:]))
48
+ else:
49
+ adjusted_sentences.append(sentence)
50
+
51
+ return ". ".join(adjusted_sentences)
52
+
53
+ def add_rhetorical_asides(output_text):
54
+ """
55
+ Insert rhetorical asides or relatable human phrases into the text.
56
+ """
57
+ asides = [
58
+ "Don’t you think?", "Isn't that interesting?",
59
+ "You see what I mean?", "Crazy, right?", "Makes sense, doesn't it?"
60
+ ]
61
+ sentences = output_text.split(". ")
62
+ modified_sentences = [
63
+ f"{sentence} {random.choice(asides)}" if random.random() < 0.2 else sentence
64
+ for sentence in sentences
65
+ ]
66
+ return ". ".join(modified_sentences)
67
+
68
  def refine_humanization(output_text, tone):
69
  """
70
  Refine text further for deeper humanization and introduce natural flow with slight noise.
71
  """
72
  refinement_prompt = (
73
+ f"Take the following text and make it truly human-like, smooth, and conversational. "
74
+ f"Ensure it uses natural language, rhetorical questions, and slight imperfections. "
75
+ f"The tone should be {tone}. Here's the text: {output_text}"
76
  )
77
  try:
78
  refinement_response = client.chat.completions.create(
 
81
  stream=False,
82
  )
83
  refined_text = refinement_response.choices[0].message.content.strip()
84
+ refined_text = add_human_noise(refined_text)
85
+ refined_text = adjust_sentence_structure(refined_text)
86
+ return refined_text
87
  except Exception:
88
  return output_text # Return original output if refinement fails
89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  def split_text_into_chunks(text, max_words=500):
91
  """
92
  Split text into chunks of a maximum number of words.
 
109
  ("Humanize Text", "Rephrase Text"),
110
  index=0
111
  )
 
 
112
  tone = st.selectbox("Select tone (for humanizing):", ["Casual", "Professional", "Neutral", "Engaging", "Friendly"])
113
 
114
+ # Depth of Humanization
115
+ humanization_depth = st.slider(
116
+ "Select depth of humanization:",
117
+ min_value=1,
118
+ max_value=5,
119
+ value=3,
120
+ help="Higher values introduce more layers of human-like imperfections."
121
+ )
122
+
123
  # Generate Output
124
  if st.button("Generate Output"):
125
  if not input_text.strip():
 
148
  stream=False,
149
  )
150
 
151
+ # Extract and refine output
152
  output_text = chat_completion.choices[0].message.content.strip()
153
+ for _ in range(humanization_depth):
154
+ output_text = refine_humanization(output_text, tone)
155
  output_chunks.append(output_text)
156
 
157
  # Combine processed chunks
158
  final_output = " ".join(output_chunks)
159
+ final_output = add_rhetorical_asides(final_output)
160
  output_word_count = len(final_output.split())
161
 
162
  # Display Output