Spaces:
Sleeping
Sleeping
Ankit Singh
commited on
Commit
·
6ef5dd5
1
Parent(s):
1373670
Added Arogya MED App
Browse files- app.py +128 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from groq import Groq
|
3 |
+
|
4 |
+
# Function to validate the API key entered by the user
|
5 |
+
def validate_api_key(api_key):
|
6 |
+
try:
|
7 |
+
# Initialize the Groq client with the provided API key
|
8 |
+
client = Groq(api_key=api_key)
|
9 |
+
# Perform a minimal valid API call
|
10 |
+
response = client.chat.completions.create(
|
11 |
+
model="llama-3.1-70b-versatile",
|
12 |
+
messages=[
|
13 |
+
{"role": "system", "content": "Validation test for API key."},
|
14 |
+
{"role": "user", "content": "Hello, world!"}
|
15 |
+
],
|
16 |
+
temperature=0.1,
|
17 |
+
max_tokens=5,
|
18 |
+
)
|
19 |
+
if response and response.choices:
|
20 |
+
return True, "API key is valid!"
|
21 |
+
else:
|
22 |
+
return False, "API key validation failed: Empty response."
|
23 |
+
except Exception as e:
|
24 |
+
return False, f"Invalid API key. Error: {str(e)}"
|
25 |
+
|
26 |
+
# Blog content generation function
|
27 |
+
def generate_blog_content(topic, api_key):
|
28 |
+
client = Groq(api_key=api_key)
|
29 |
+
|
30 |
+
prompt = f"""Generate a blog post on '{topic}' with the following structure:
|
31 |
+
|
32 |
+
1. **Create 20 sections**, with a **FAQs section as the last one**.
|
33 |
+
2. **Use <section> tags** for sections, with <h2> for headings and <p> for content.
|
34 |
+
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).
|
35 |
+
4. **Highlight key terms** with <strong> tags for clarity.
|
36 |
+
5. Each section should be **250-300 words** and directly relate to the topic.
|
37 |
+
6. Write in a **formal tone** suitable for a professional medical audience.
|
38 |
+
7. Avoid single-word headings and ensure every paragraph adds informative value.
|
39 |
+
8. FAQs should address potential reader questions based on the main content.
|
40 |
+
|
41 |
+
**Example:**
|
42 |
+
<section>
|
43 |
+
<h2>Overview of [Medical Condition]</h2>
|
44 |
+
<p>Explanation with <strong>key terms</strong> highlighted...</p>
|
45 |
+
</section>
|
46 |
+
|
47 |
+
<section>
|
48 |
+
<h2>FAQs</h2>
|
49 |
+
<h3>What causes [Medical Condition]?</h3>
|
50 |
+
<p>A clear, accurate answer with <strong>relevant terms</strong> highlighted.</p>
|
51 |
+
<h3>How is [Medical Condition] treated?</h3>
|
52 |
+
<p>Explanation of treatment options, with <strong>important terms</strong> highlighted...</p>
|
53 |
+
</section>
|
54 |
+
"""
|
55 |
+
|
56 |
+
try:
|
57 |
+
completion = client.chat.completions.create(
|
58 |
+
model="llama-3.1-70b-versatile", # Groq's Mixtral model
|
59 |
+
messages=[
|
60 |
+
{"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."},
|
61 |
+
{"role": "user", "content": prompt}
|
62 |
+
],
|
63 |
+
temperature=0.5,
|
64 |
+
max_tokens=8000,
|
65 |
+
)
|
66 |
+
return completion.choices[0].message.content
|
67 |
+
except Exception as e:
|
68 |
+
return f"Error generating content: {str(e)}"
|
69 |
+
|
70 |
+
# Callback function for login
|
71 |
+
def on_login(api_key):
|
72 |
+
is_valid, message = validate_api_key(api_key)
|
73 |
+
if is_valid:
|
74 |
+
return api_key, gr.update(visible=False), gr.update(visible=True), gr.Markdown(f"**Success!** {message}")
|
75 |
+
else:
|
76 |
+
return "", gr.update(visible=True), gr.update(visible=False), gr.Markdown(f"**Error!** {message}", visible=True)
|
77 |
+
|
78 |
+
# Callback function for generating content
|
79 |
+
def on_generate_click(topic, api_key):
|
80 |
+
if not api_key:
|
81 |
+
return "Error: API key not provided.", ""
|
82 |
+
if not topic:
|
83 |
+
return "Please enter a blog topic.", ""
|
84 |
+
content = generate_blog_content(topic, api_key)
|
85 |
+
return content, content
|
86 |
+
|
87 |
+
# Gradio app
|
88 |
+
with gr.Blocks(title="ArogyaJivan") as demo:
|
89 |
+
|
90 |
+
# Login Section
|
91 |
+
with gr.Column(visible=True) as login_section:
|
92 |
+
gr.Markdown("## Login to Access the Blog Generator")
|
93 |
+
api_key_input = gr.Textbox(label="Enter your API Key:", type="password")
|
94 |
+
login_button = gr.Button("Login")
|
95 |
+
login_feedback = gr.Markdown(visible=False)
|
96 |
+
gr.Markdown("""
|
97 |
+
### How to Get an API Key
|
98 |
+
- Visit [Groq API Key Portal](https://groq.com/get-api-key)
|
99 |
+
- Sign up or log in to your account.
|
100 |
+
- Navigate to the **API Keys** section and generate a new key.
|
101 |
+
- Copy the API key and paste it Above.
|
102 |
+
""")
|
103 |
+
|
104 |
+
|
105 |
+
# Blog Content Generator Section
|
106 |
+
with gr.Column(visible=False) as blog_section:
|
107 |
+
gr.Markdown("# Blog Content Generator")
|
108 |
+
with gr.Column():
|
109 |
+
topic_input = gr.Textbox(label="Enter your blog topic:")
|
110 |
+
generate_button = gr.Button("Generate Blog Content")
|
111 |
+
with gr.Row():
|
112 |
+
output_html = gr.Code(label="Generated HTML Content:", language='html')
|
113 |
+
output_preview = gr.HTML(label="Preview:")
|
114 |
+
|
115 |
+
state = gr.State() # Manage API key state
|
116 |
+
|
117 |
+
login_button.click(
|
118 |
+
on_login,
|
119 |
+
inputs=api_key_input,
|
120 |
+
outputs=[state, login_section, blog_section, login_feedback]
|
121 |
+
)
|
122 |
+
generate_button.click(
|
123 |
+
on_generate_click,
|
124 |
+
inputs=[topic_input, state],
|
125 |
+
outputs=[output_html, output_preview]
|
126 |
+
)
|
127 |
+
|
128 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
groq
|