ciaochris commited on
Commit
77ad748
·
verified ·
1 Parent(s): ac95e2d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import logging
3
+ import gradio as gr
4
+ from groq import Groq
5
+
6
+ # Initialize logging
7
+ logging.basicConfig(level=logging.INFO)
8
+
9
+ # Initialize Groq client
10
+ try:
11
+ client = Groq(api_key=os.environ["GROQ_API_KEY"])
12
+ except KeyError:
13
+ logging.error("GROQ_API_KEY not found in environment variables")
14
+ raise EnvironmentError("Please set the GROQ_API_KEY environment variable")
15
+
16
+ # Specify the actual model name
17
+ GROQ_MODEL = "mixtral-8x7b-32768" # Replace with the actual model you want to use
18
+
19
+ SYSTEM_PROMPT = """
20
+ You are an expert in natural language processing and writing, specializing in transforming AI-generated text into highly natural, human-like prose. Your task is to refine and humanize the given text while adhering to the following guidelines:
21
+
22
+ 1. Preserve the core meaning and information of the original text.
23
+ 2. Maintain the overall structure, including paragraph breaks and any headings or subheadings.
24
+ 3. Adjust the writing style to be more natural and conversational, as if written by a skilled human writer.
25
+ 4. Vary sentence structure, using a mix of simple, compound, and complex sentences to improve flow and readability.
26
+ 5. Introduce subtle imperfections that are characteristic of human writing, such as:
27
+ - Occasional use of contractions (e.g., "it's", "don't", "we're")
28
+ - Varied paragraph lengths
29
+ - Strategic use of transition words and phrases
30
+ - Occasional parenthetical asides or brief digressions
31
+ - Subtle voice and tone shifts that maintain consistency but add depth
32
+
33
+ 6. Refine vocabulary choices to be more nuanced and context-appropriate, avoiding overly formal or repetitive language.
34
+ 7. Ensure proper use of idioms, colloquialisms, and figures of speech where appropriate, but don't overuse them.
35
+ 8. Maintain consistent tense and point of view throughout the text.
36
+ 9. Preserve any technical terms, proper nouns, or specific jargon present in the original text.
37
+ 10. Aim to keep the word count within 5% of the original text.
38
+
39
+ Remember, the goal is to make the text feel authentically human-written while retaining its original purpose and information. Strive for a balance between polish and natural imperfection that characterizes high-quality human writing.
40
+ """
41
+
42
+ USER_PROMPT = """
43
+ Please humanize the following text, applying the guidelines provided. Ensure the output reads as if it were written by a skilled human writer, with natural flow and subtle imperfections. Maintain the original meaning and structure while enhancing its overall quality and readability.
44
+
45
+ Original text:
46
+ {input_text}
47
+
48
+ Humanized version:
49
+ """
50
+
51
+ def humanize_text(AI_text):
52
+ try:
53
+ response = client.chat.completions.create(
54
+ model=GROQ_MODEL,
55
+ temperature=0.7,
56
+ max_tokens=2000,
57
+ messages=[
58
+ {"role": "system", "content": SYSTEM_PROMPT},
59
+ {"role": "user", "content": USER_PROMPT.format(input_text=AI_text)}
60
+ ]
61
+ )
62
+ return response.choices[0].message.content.strip()
63
+ except Exception as e:
64
+ logging.error(f"Error during humanization: {e}")
65
+ return "An error occurred while processing the text."
66
+
67
+ def main_function(AI_text):
68
+ return humanize_text(AI_text)
69
+
70
+ # Gradio interface definition
71
+ interface = gr.Interface(
72
+ fn=main_function,
73
+ inputs=gr.Textbox(label="Input AI-generated text", lines=10),
74
+ outputs=gr.Textbox(label="Human Touch👈", lines=10),
75
+ title="HumanTouch App",
76
+ description="Transform AI-generated text into a captivating human-crafted masterpiece!",
77
+ examples=[
78
+ ["The artificial intelligence system processed the data and concluded that the optimal solution was to implement a new algorithm for efficient resource allocation."],
79
+ ["The robotic assistant scanned the room, identifying objects and their locations with precise accuracy, before proceeding to execute its predetermined cleaning routine."]
80
+ ]
81
+ )
82
+
83
+ # Launch the Gradio app
84
+ if __name__ == "__main__":
85
+ interface.launch(debug=True)