File size: 4,144 Bytes
0efc631
 
 
 
 
4a3065e
 
 
0efc631
4a3065e
 
 
 
0efc631
 
 
 
 
4a3065e
0efc631
 
 
 
 
 
 
 
928686b
0efc631
 
4a3065e
0efc631
 
 
 
 
 
 
 
 
 
4a3065e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0efc631
4a3065e
 
 
0efc631
4a3065e
 
 
 
 
 
0efc631
4a3065e
 
 
 
 
 
0efc631
 
 
4a3065e
0efc631
 
 
 
4a3065e
0efc631
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import gradio as gr
import os
from groq import Groq
import markdown

# Retrieve system prompts from environment variables
GENERATE_PROMPT = os.environ.get("system_prompt")
HUMANIZE_PROMPT = os.environ.get("HUMANIZE_PROMPT")

def generate_mba_content(topic, api_key):
    if not GENERATE_PROMPT:
        return "Error: Generate prompt not found. Ensure 'GENERATE_PROMPT' is set in your environment."
    
    try:
        client = Groq(api_key=api_key)
    except Exception as e:
        return f"Error: Failed to initialize Groq client. Check your API key. Details: {str(e)}"

    prompt = GENERATE_PROMPT.replace("[TOPIC]", topic)
    try:
        response = client.chat.completions.create(
            model="llama3-70b-8192",
            messages=[
                {"role": "system", "content": prompt},
                {"role": "user", "content": f"Generate content for the topic: {topic}"}
            ],
            temperature=0.7,
            max_tokens=4000
        )
        content = response.choices[0].message.content
        content = content.replace("—", ",")  # Replace em dashes with commas
        if not content.startswith("#"):
            content = markdown.markdown(content)
        return content
    except Exception as e:
        return f"Error: Failed to generate content. Details: {str(e)}"
    finally:
        api_key = None
        if 'client' in locals():
            del client

def humanize_text(text, api_key):
    if not HUMANIZE_PROMPT:
        return "Error: Humanize prompt not found. Ensure 'HUMANIZE_PROMPT' is set in your environment."
    
    try:
        client = Groq(api_key=api_key)
    except Exception as e:
        return f"Error: Failed to initialize Groq client. Check your API key. Details: {str(e)}"

    prompt = HUMANIZE_PROMPT.replace("[TEXT]", text)
    try:
        response = client.chat.completions.create(
            model="llama3-70b-8192",
            messages=[
                {"role": "system", "content": prompt},
                {"role": "user", "content": f"Rewrite the following text: {text}"}
            ],
            temperature=0.7,
            max_tokens=4000
        )
        content = response.choices[0].message.content
        content = content.replace("—", ",")  # Replace em dashes with commas
        if not content.startswith("#"):
            content = markdown.markdown(content)
        return content
    except Exception as e:
        return f"Error: Failed to humanize text. Details: {str(e)}"
    finally:
        api_key = None
        if 'client' in locals():
            del client

# Define Gradio interface with tabs
def gradio_app():
    with gr.Blocks(title="MBA Content Tools") as app:
        gr.Markdown("# MBA Content Tools")
        gr.Markdown("Generate or humanize content in MBA-style format.")
        
        with gr.Tab("Generate Content from Topic"):
            topic_input = gr.Textbox(label="Topic", placeholder="e.g., Strategic Management")
            api_key_gen = gr.Textbox(label="Groq API Key", type="password")
            generate_btn = gr.Button("Generate Content")
            generate_output = gr.Markdown(label="Generated Content")
            generate_btn.click(generate_mba_content, inputs=[topic_input, api_key_gen], outputs=generate_output)
        
        with gr.Tab("Humanize Existing Text"):
            text_input = gr.TextArea(label="Text to Humanize", placeholder="Paste your article, report, or paragraph here")
            api_key_hum = gr.Textbox(label="Groq API Key", type="password")
            humanize_btn = gr.Button("Humanize Text")
            humanize_output = gr.Markdown(label="Humanized Content")
            humanize_btn.click(humanize_text, inputs=[text_input, api_key_hum], outputs=humanize_output)
        
        gr.Markdown("""
        **Note:** Your API key is used securely for this session and cleared from memory afterward. 
        Ensure the environment variables 'GENERATE_PROMPT' and 'HUMANIZE_PROMPT' are set securely in your deployment configuration.
        """)
    
    return app

# Launch the app
if __name__ == "__main__":
    app = gradio_app()
    app.launch()