Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -39,28 +39,64 @@ def generate_mba_content(topic, api_key):
|
|
| 39 |
if 'client' in locals():
|
| 40 |
del client
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
def humanize_text(text, api_key):
|
|
|
|
|
|
|
|
|
|
| 43 |
if not HUMANIZE_PROMPT:
|
| 44 |
return "Error: Humanize prompt not found. Ensure 'HUMANIZE_PROMPT' is set in your environment."
|
| 45 |
-
|
| 46 |
try:
|
| 47 |
client = Groq(api_key=api_key)
|
| 48 |
except Exception as e:
|
| 49 |
return f"Error: Failed to initialize Groq client. Check your API key. Details: {str(e)}"
|
| 50 |
|
| 51 |
-
prompt
|
|
|
|
| 52 |
try:
|
| 53 |
response = client.chat.completions.create(
|
| 54 |
model="llama3-70b-8192",
|
| 55 |
messages=[
|
| 56 |
-
{"role": "system", "content":
|
| 57 |
-
{"role": "user", "content":
|
| 58 |
],
|
| 59 |
temperature=0.7,
|
| 60 |
max_tokens=4000,
|
| 61 |
)
|
| 62 |
content = response.choices[0].message.content
|
| 63 |
-
content = content.replace("—", ",")
|
| 64 |
if not content.startswith("#"):
|
| 65 |
content = markdown.markdown(content)
|
| 66 |
return content
|
|
|
|
| 39 |
if 'client' in locals():
|
| 40 |
del client
|
| 41 |
|
| 42 |
+
# def humanize_text(text, api_key):
|
| 43 |
+
# if not HUMANIZE_PROMPT:
|
| 44 |
+
# return "Error: Humanize prompt not found. Ensure 'HUMANIZE_PROMPT' is set in your environment."
|
| 45 |
+
|
| 46 |
+
# try:
|
| 47 |
+
# client = Groq(api_key=api_key)
|
| 48 |
+
# except Exception as e:
|
| 49 |
+
# return f"Error: Failed to initialize Groq client. Check your API key. Details: {str(e)}"
|
| 50 |
+
|
| 51 |
+
# prompt = HUMANIZE_PROMPT.replace("[TEXT]", text)
|
| 52 |
+
# try:
|
| 53 |
+
# response = client.chat.completions.create(
|
| 54 |
+
# model="llama3-70b-8192",
|
| 55 |
+
# messages=[
|
| 56 |
+
# {"role": "system", "content": prompt},
|
| 57 |
+
# {"role": "user", "content": f"Rewrite the following text: {text}"}
|
| 58 |
+
# ],
|
| 59 |
+
# temperature=0.7,
|
| 60 |
+
# max_tokens=4000,
|
| 61 |
+
# )
|
| 62 |
+
# content = response.choices[0].message.content
|
| 63 |
+
# content = content.replace("—", ",") # Replace em dashes with commas
|
| 64 |
+
# if not content.startswith("#"):
|
| 65 |
+
# content = markdown.markdown(content)
|
| 66 |
+
# return content
|
| 67 |
+
# except Exception as e:
|
| 68 |
+
# return f"Error: Failed to humanize text. Details: {str(e)}"
|
| 69 |
+
# finally:
|
| 70 |
+
# api_key = None
|
| 71 |
+
# if 'client' in locals():
|
| 72 |
+
# del client
|
| 73 |
+
|
| 74 |
def humanize_text(text, api_key):
|
| 75 |
+
SYSTEM_PROMPT = "You are a MBA teacher as well as a content writer. Your task is to rewrite provided content to sound more natural and human while following instructions."
|
| 76 |
+
# HUMANIZE_PROMPT = os.environ.get("HUMANIZE_CONTENT_2") # Ex: "Rewrite the following text to be more human: [TEXT]"
|
| 77 |
+
|
| 78 |
if not HUMANIZE_PROMPT:
|
| 79 |
return "Error: Humanize prompt not found. Ensure 'HUMANIZE_PROMPT' is set in your environment."
|
| 80 |
+
|
| 81 |
try:
|
| 82 |
client = Groq(api_key=api_key)
|
| 83 |
except Exception as e:
|
| 84 |
return f"Error: Failed to initialize Groq client. Check your API key. Details: {str(e)}"
|
| 85 |
|
| 86 |
+
# Place the input text into the prompt template for the user message
|
| 87 |
+
user_prompt = HUMANIZE_PROMPT.replace("[TEXT]", text)
|
| 88 |
try:
|
| 89 |
response = client.chat.completions.create(
|
| 90 |
model="llama3-70b-8192",
|
| 91 |
messages=[
|
| 92 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 93 |
+
{"role": "user", "content": user_prompt}
|
| 94 |
],
|
| 95 |
temperature=0.7,
|
| 96 |
max_tokens=4000,
|
| 97 |
)
|
| 98 |
content = response.choices[0].message.content
|
| 99 |
+
content = content.replace("—", ",")
|
| 100 |
if not content.startswith("#"):
|
| 101 |
content = markdown.markdown(content)
|
| 102 |
return content
|