tushar310 commited on
Commit
4a3065e
·
verified ·
1 Parent(s): e30d665

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -36
app.py CHANGED
@@ -1,24 +1,23 @@
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.environ["system_prompt"]
10
 
11
- # Initialize Groq client with user-provided API key
 
 
 
12
  try:
13
  client = Groq(api_key=api_key)
14
  except Exception as e:
15
  return f"Error: Failed to initialize Groq client. Check your API key. Details: {str(e)}"
16
 
17
- # Replace [TOPIC] in the system prompt with the user-provided topic
18
- prompt = system_prompt.replace("[TOPIC]", topic)
19
-
20
  try:
21
- # Generate content using Groq API
22
  response = client.chat.completions.create(
23
  model="llama3-70b-8192",
24
  messages=[
@@ -29,53 +28,77 @@ def generate_mba_content(topic, api_key):
29
  max_tokens=4000
30
  )
31
  content = response.choices[0].message.content
32
- # Convert to markdown if not already in the correct format
33
  if not content.startswith("#"):
34
  content = markdown.markdown(content)
35
  return content
36
  except Exception as e:
37
  return f"Error: Failed to generate content. Details: {str(e)}"
38
  finally:
39
- # Clear API key from memory
40
  api_key = None
41
  if 'client' in locals():
42
  del client
43
 
44
- # Define Gradio interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  def gradio_app():
46
- with gr.Blocks(title="MBA Content Generator") as app:
47
- gr.Markdown("# MBA Content Generator")
48
- gr.Markdown("Enter a topic and your Groq API key to generate MBA-style content in markdown format.")
49
 
50
- topic_input = gr.Textbox(
51
- label="Topic",
52
- placeholder="e.g., Strategic Management, Supply Chain Optimization",
53
- lines=1
54
- )
55
- api_key_input = gr.Textbox(
56
- label="Groq API Key",
57
- placeholder="Enter your Groq API key here",
58
- type="password",
59
- lines=1
60
- )
61
- output = gr.Markdown(label="Generated Content")
62
- submit_btn = gr.Button("Generate Content")
63
 
64
- submit_btn.click(
65
- fn=generate_mba_content,
66
- inputs=[topic_input, api_key_input],
67
- outputs=output
68
- )
 
69
 
70
  gr.Markdown("""
71
  **Note:** Your API key is used securely for this session and cleared from memory afterward.
72
- This app is designed for deployment on Hugging Face Spaces. Ensure the environment variable
73
- `MBA_SYSTEM_PROMPT` is set securely in your deployment configuration.
74
  """)
75
 
76
  return app
77
 
78
- # Launch the app (for local testing; Hugging Face Spaces handles deployment)
79
  if __name__ == "__main__":
80
  app = gradio_app()
81
  app.launch()
 
1
  import gradio as gr
2
  import os
3
  from groq import Groq
 
4
  import markdown
5
 
6
+ # Retrieve system prompts from environment variables
7
+ GENERATE_PROMPT = os.environ.get("system_prompt")
8
+ HUMANIZE_PROMPT = os.environ.get("HUMANIZE_PROMPT")
9
 
10
+ def generate_mba_content(topic, api_key):
11
+ if not GENERATE_PROMPT:
12
+ return "Error: Generate prompt not found. Ensure 'GENERATE_PROMPT' is set in your environment."
13
+
14
  try:
15
  client = Groq(api_key=api_key)
16
  except Exception as e:
17
  return f"Error: Failed to initialize Groq client. Check your API key. Details: {str(e)}"
18
 
19
+ prompt = GENERATE_PROMPT.replace("[TOPIC]", topic)
 
 
20
  try:
 
21
  response = client.chat.completions.create(
22
  model="llama3-70b-8192",
23
  messages=[
 
28
  max_tokens=4000
29
  )
30
  content = response.choices[0].message.content
31
+ content = content.replace("—", ",") # Replace em dashes with commas
32
  if not content.startswith("#"):
33
  content = markdown.markdown(content)
34
  return content
35
  except Exception as e:
36
  return f"Error: Failed to generate content. Details: {str(e)}"
37
  finally:
 
38
  api_key = None
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
+ # Define Gradio interface with tabs
75
  def gradio_app():
76
+ with gr.Blocks(title="MBA Content Tools") as app:
77
+ gr.Markdown("# MBA Content Tools")
78
+ gr.Markdown("Generate or humanize content in MBA-style format.")
79
 
80
+ with gr.Tab("Generate Content from Topic"):
81
+ topic_input = gr.Textbox(label="Topic", placeholder="e.g., Strategic Management")
82
+ api_key_gen = gr.Textbox(label="Groq API Key", type="password")
83
+ generate_btn = gr.Button("Generate Content")
84
+ generate_output = gr.Markdown(label="Generated Content")
85
+ generate_btn.click(generate_mba_content, inputs=[topic_input, api_key_gen], outputs=generate_output)
 
 
 
 
 
 
 
86
 
87
+ with gr.Tab("Humanize Existing Text"):
88
+ text_input = gr.TextArea(label="Text to Humanize", placeholder="Paste your article, report, or paragraph here")
89
+ api_key_hum = gr.Textbox(label="Groq API Key", type="password")
90
+ humanize_btn = gr.Button("Humanize Text")
91
+ humanize_output = gr.Markdown(label="Humanized Content")
92
+ humanize_btn.click(humanize_text, inputs=[text_input, api_key_hum], outputs=humanize_output)
93
 
94
  gr.Markdown("""
95
  **Note:** Your API key is used securely for this session and cleared from memory afterward.
96
+ Ensure the environment variables 'GENERATE_PROMPT' and 'HUMANIZE_PROMPT' are set securely in your deployment configuration.
 
97
  """)
98
 
99
  return app
100
 
101
+ # Launch the app
102
  if __name__ == "__main__":
103
  app = gradio_app()
104
  app.launch()