Ankit Singh
Updated prompt
984e43e
raw
history blame
5.29 kB
import gradio as gr
from groq import Groq
# Function to validate the API key entered by the user
def validate_api_key(api_key):
try:
# Initialize the Groq client with the provided API key
client = Groq(api_key=api_key)
# Perform a minimal valid API call
response = client.chat.completions.create(
model="llama-3.1-70b-versatile",
messages=[
{"role": "system", "content": "Validation test for API key."},
{"role": "user", "content": "Hello, world!"}
],
temperature=0.1,
max_tokens=5,
)
if response and response.choices:
return True, "API key is valid!"
else:
return False, "API key validation failed: Empty response."
except Exception as e:
return False, f"Invalid API key. Error: {str(e)}"
# Blog content generation function
def generate_blog_content(topic, api_key):
client = Groq(api_key=api_key)
prompt = f"""Generate a blog post on '{topic}' with the following structure:
1. Create 20 sections, with a FAQs section as the last one.
2. Use <section> tags for sections, with <h2> for headings and <p> for content.
3. If there are sub-headings under an <h2> tag, use <h3> for those sub-headings (e.g., in FAQs or where additional structure is needed).
4. Highlight key terms or keywords with <strong> tags or <b> tag for clarity and easy to read.
5. Each section should be 150-200 words and directly relate to the topic, with no unnecessary unasked responses.
6. Write in a formal tone suitable for a professional medical audience.
7. Avoid single-word or two word headings (H2 TAG) , make every h2 tag should contain keywords and ensure every paragraph adds informative value.
8. FAQs should address potential reader questions based on the main content.
Example:
<section>
<h2>Overview of [Medical Condition]</h2>
<p>Explanation with <strong>key terms</strong> highlighted...</p>
</section>
<section>
<h2>FAQs</h2>
<h3>What causes [Medical Condition]?</h3>
<p>A clear, accurate answer with <strong>relevant terms</strong> highlighted. And <strong>Keywords</strong> should also be highlited to Optime page SEO but dont make it too dense.</p>
<h3>How is [Medical Condition] treated?</h3>
<p>Explanation of treatment options, with <strong>important terms</strong> highlighted...</p>
</section>
"""
try:
completion = client.chat.completions.create(
model="llama-3.1-70b-versatile", # Groq's Mixtral model
messages=[
{"role": "system", "content": "You are a medical content specialist experienced in healthcare writing for patients and medical professionals in India. Your writing should be clear, accurate, and suitable for both educated laypersons and healthcare practitioners."},
{"role": "user", "content": prompt}
],
temperature=0.5,
max_tokens=8000,
)
return completion.choices[0].message.content
except Exception as e:
return f"Error generating content: {str(e)}"
# Callback function for login
def on_login(api_key):
is_valid, message = validate_api_key(api_key)
if is_valid:
return api_key, gr.update(visible=False), gr.update(visible=True), gr.Markdown(f"**Success!** {message}")
else:
return "", gr.update(visible=True), gr.update(visible=False), gr.Markdown(f"**Error!** {message}", visible=True)
# Callback function for generating content
def on_generate_click(topic, api_key):
if not api_key:
return "Error: API key not provided.", ""
if not topic:
return "Please enter a blog topic.", ""
content = generate_blog_content(topic, api_key)
return content, content
# Gradio app
with gr.Blocks(title="ArogyaJivan") as demo:
# Login Section
with gr.Column(visible=True) as login_section:
gr.Markdown("## Login to Access the Blog Generator")
api_key_input = gr.Textbox(label="Enter your API Key:", type="password")
login_button = gr.Button("Login")
login_feedback = gr.Markdown(visible=False)
gr.Markdown("""
### How to Get an API Key
- Visit [Groq API Key Portal](https://console.groq.com/keys)
- Sign up or log in to your account.
- Navigate to the **API Keys** section and generate a new key.
- Copy the API key and paste it Above.
""")
# Blog Content Generator Section
with gr.Column(visible=False) as blog_section:
gr.Markdown("# Arogya Blog Generator")
with gr.Column():
topic_input = gr.Textbox(label="Enter your blog topic:")
generate_button = gr.Button("Generate Blog Content")
with gr.Row():
output_html = gr.Code(label="Generated HTML Content:", language='html')
output_preview = gr.HTML(label="Preview:")
state = gr.State() # Manage API key state
login_button.click(
on_login,
inputs=api_key_input,
outputs=[state, login_section, blog_section, login_feedback]
)
generate_button.click(
on_generate_click,
inputs=[topic_input, state],
outputs=[output_html, output_preview]
)
demo.launch()