Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
from groq import Groq
|
4 |
+
import uuid
|
5 |
+
import markdown
|
6 |
+
|
7 |
+
def generate_mba_content(topic, api_key):
|
8 |
+
# Setting up the system prompt (assumed to be securely stored in environment variable)
|
9 |
+
system_prompt = os.getenv("MBA_SYSTEM_PROMPT", """
|
10 |
+
Generate content in markdown format for an MBA student on the topic of [TOPIC]. The content should feel human-written, as if crafted by an MBA student for a submission, presentation, or casual peer discussion. Follow these rules:
|
11 |
+
|
12 |
+
Persona: Write as an MBA student sharing insights with classmates. Keep the tone casual and friendly, like a conversation over pizza after class. Add a touch of humor or a personal story where it fits.
|
13 |
+
|
14 |
+
Constraints:
|
15 |
+
- No em dashes. Use commas, parentheses, or colons instead.
|
16 |
+
- Skip stiff phrases like "It is imperative" or "This paper will explore". Go for relaxed options like "Here’s the deal" or "Let’s dig into this".
|
17 |
+
- Limit transition words (e.g., "moreover", "thus").
|
18 |
+
- Avoid these terms: aligns, aims to explore, today’s fast-paced world, notable works include, notable figures, surpassing, tragically, impacting, making an impact, research needed to understand, despite facing, expressed excitement, evolving situation, delve, harnessing, at the heart of, in essence, facilitating, intrinsic, integral, core, facet, nuance, culmination, manifestation, inherent, confluence, underlying, intricacies, epitomize, embodiment, iteration, synthesize, amplify, impetus, catalyst, synergy, cohesive, paradigm, dynamics, implications, prerequisite, fusion, holistic, quintessential, cohesion, symbiosis, integration, encompass, unveil, unravel, emanate, illuminate, reverberate, augment, infuse, extrapolate, embody, unify, inflection, instigate, embark, envisage, elucidate, substantiate, resonate, catalyze, resilience, evoke, pinnacle, evolve, digital bazaar, tapestry, leverage, centerpiece, subtlety, immanent, exemplify, blend, comprehensive, archetypal, unity, harmony, conceptualize, reinforce, mosaic, catering.
|
19 |
+
|
20 |
+
Style:
|
21 |
+
- Mix up sentence lengths: some short and snappy, others longer with detail.
|
22 |
+
- Use contractions (e.g., can’t, you’re) and casual phrases that sound natural.
|
23 |
+
- Stick to active voice (e.g., "I learned" not "It was learned").
|
24 |
+
- Add sensory details (e.g., "The coffee burned my tongue while I scrambled to finish the slide deck").
|
25 |
+
- Include a personal anecdote, hypothetical situation, or relatable example (e.g., a chaotic group project or a real-world internship moment).
|
26 |
+
|
27 |
+
Structure:
|
28 |
+
- Introduction: Kick off with a question, a fun fact, or a relatable moment (e.g., pulling an all-nighter before a deadline).
|
29 |
+
- Body: Use markdown subheadings (e.g., ## My Take) if needed. Include 1-2 anecdotes, scenarios, or examples to back up your points.
|
30 |
+
- Conclusion: Wrap up with practical advice or a reflective thought that sticks with the reader.
|
31 |
+
|
32 |
+
Examples:
|
33 |
+
Unacceptable: "In today’s fast-paced world, leveraging technology is key, furthermore, it facilitates synergy across teams."
|
34 |
+
Acceptable: "Tech’s a game-changer, no doubt. Last semester, my team used Slack to avoid those endless email chains, and we actually got stuff done without losing our minds."
|
35 |
+
|
36 |
+
Output the content in markdown format.
|
37 |
+
""")
|
38 |
+
|
39 |
+
# Initialize Groq client with user-provided API key
|
40 |
+
try:
|
41 |
+
client = Groq(api_key=api_key)
|
42 |
+
except Exception as e:
|
43 |
+
return f"Error: Failed to initialize Groq client. Check your API key. Details: {str(e)}"
|
44 |
+
|
45 |
+
# Replace [TOPIC] in the system prompt with the user-provided topic
|
46 |
+
prompt = system_prompt.replace("[TOPIC]", topic)
|
47 |
+
|
48 |
+
try:
|
49 |
+
# Generate content using Groq API
|
50 |
+
response = client.chat.completions.create(
|
51 |
+
model="llama3-70b-8192",
|
52 |
+
messages=[
|
53 |
+
{"role": "system", "content": prompt},
|
54 |
+
{"role": "user", "content": f"Generate content for the topic: {topic}"}
|
55 |
+
],
|
56 |
+
temperature=0.7,
|
57 |
+
max_tokens=1000
|
58 |
+
)
|
59 |
+
content = response.choices[0].message.content
|
60 |
+
# Convert to markdown if not already in the correct format
|
61 |
+
if not content.startswith("#"):
|
62 |
+
content = markdown.markdown(content)
|
63 |
+
return content
|
64 |
+
except Exception as e:
|
65 |
+
return f"Error: Failed to generate content. Details: {str(e)}"
|
66 |
+
finally:
|
67 |
+
# Clear API key from memory
|
68 |
+
api_key = None
|
69 |
+
if 'client' in locals():
|
70 |
+
del client
|
71 |
+
|
72 |
+
# Define Gradio interface
|
73 |
+
def gradio_app():
|
74 |
+
with gr.Blocks(title="MBA Content Generator") as app:
|
75 |
+
gr.Markdown("# MBA Content Generator")
|
76 |
+
gr.Markdown("Enter a topic and your Groq API key to generate MBA-style content in markdown format.")
|
77 |
+
|
78 |
+
topic_input = gr.Textbox(
|
79 |
+
label="Topic",
|
80 |
+
placeholder="e.g., Strategic Management, Supply Chain Optimization",
|
81 |
+
lines=1
|
82 |
+
)
|
83 |
+
api_key_input = gr.Textbox(
|
84 |
+
label="Groq API Key",
|
85 |
+
placeholder="Enter your Groq API key here",
|
86 |
+
type="password",
|
87 |
+
lines=1
|
88 |
+
)
|
89 |
+
output = gr.Markdown(label="Generated Content")
|
90 |
+
submit_btn = gr.Button("Generate Content")
|
91 |
+
|
92 |
+
submit_btn.click(
|
93 |
+
fn=generate_mba_content,
|
94 |
+
inputs=[topic_input, api_key_input],
|
95 |
+
outputs=output
|
96 |
+
)
|
97 |
+
|
98 |
+
gr.Markdown("""
|
99 |
+
**Note:** Your API key is used securely for this session and cleared from memory afterward.
|
100 |
+
This app is designed for deployment on Hugging Face Spaces. Ensure the environment variable
|
101 |
+
`MBA_SYSTEM_PROMPT` is set securely in your deployment configuration.
|
102 |
+
""")
|
103 |
+
|
104 |
+
return app
|
105 |
+
|
106 |
+
# Launch the app (for local testing; Hugging Face Spaces handles deployment)
|
107 |
+
if __name__ == "__main__":
|
108 |
+
app = gradio_app()
|
109 |
+
app.launch()
|