Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,91 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import requests
|
|
|
|
| 3 |
|
| 4 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
}
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
import requests
|
| 4 |
+
import json
|
| 5 |
|
| 6 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 7 |
+
GROQ_COMPLETION_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 8 |
+
GROQ_MODEL = "llama-3.1-8b-instant"
|
| 9 |
+
|
| 10 |
+
def groq_completion(prompt, system_prompt=None):
|
| 11 |
+
headers = {
|
| 12 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 13 |
+
"Content-Type": "application/json"
|
| 14 |
+
}
|
| 15 |
+
body = {
|
| 16 |
+
"model": GROQ_MODEL,
|
| 17 |
+
"messages": [
|
| 18 |
+
{"role": "system", "content": system_prompt or "You are a fast and reliable business email classification assistant."},
|
| 19 |
+
{"role": "user", "content": prompt}
|
| 20 |
+
],
|
| 21 |
+
"temperature": 0.3,
|
| 22 |
+
"max_tokens": 512
|
| 23 |
+
}
|
| 24 |
+
try:
|
| 25 |
+
response = requests.post(GROQ_COMPLETION_URL, headers=headers, json=body)
|
| 26 |
+
response.raise_for_status()
|
| 27 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 28 |
+
except Exception as e:
|
| 29 |
+
print("Groq API error:", e)
|
| 30 |
+
if 'response' in locals():
|
| 31 |
+
print("Groq API response text:", response.text)
|
| 32 |
+
return "Error"
|
| 33 |
+
|
| 34 |
+
def email_assistant(email_text):
|
| 35 |
+
# Clear, strict prompt for JSON extraction
|
| 36 |
+
prompt = (
|
| 37 |
+
"Analyze the following business email and extract ONLY the following elements as a compact JSON dictionary:\n"
|
| 38 |
+
"Category (Sales, Support, Spam, Finance, General),\n"
|
| 39 |
+
"Confidence (percentage),\n"
|
| 40 |
+
"Priority (High/Normal/Low),\n"
|
| 41 |
+
"Suggested Action or Forwarding Address,\n"
|
| 42 |
+
"Draft professional response,\n"
|
| 43 |
+
"Action items (bulleted list).\n\n"
|
| 44 |
+
"Return ONLY a compact JSON dictionary, nothing else:\n"
|
| 45 |
+
'{"Category":"...", "Confidence":"...", "Priority":"...", "Suggested Action":"...", "Draft Response":"...", "Action Items":"..."}\n\n'
|
| 46 |
+
f"EMAIL:\n{email_text}\n"
|
| 47 |
+
)
|
| 48 |
+
raw_result = groq_completion(prompt)
|
| 49 |
+
try:
|
| 50 |
+
output = json.loads(raw_result)
|
| 51 |
+
return [
|
| 52 |
+
output.get("Category", "Error"),
|
| 53 |
+
output.get("Confidence", "Error"),
|
| 54 |
+
output.get("Priority", "Error"),
|
| 55 |
+
output.get("Suggested Action", "Error"),
|
| 56 |
+
output.get("Draft Response", "Error"),
|
| 57 |
+
output.get("Action Items", "Error"),
|
| 58 |
+
]
|
| 59 |
+
except Exception as e:
|
| 60 |
+
print(f"Parsing or LLM output error: {e}\nRaw output: {raw_result}")
|
| 61 |
+
# Fail gracefully: show result in all fields for debugging
|
| 62 |
+
return [raw_result]*6
|
| 63 |
+
|
| 64 |
+
with gr.Blocks() as demo:
|
| 65 |
+
gr.Markdown("# Email Classification & Response Assistant (Groq powered)")
|
| 66 |
+
email_input = gr.TextArea(
|
| 67 |
+
label="Paste your email here (include Subject, From, Body if possible)",
|
| 68 |
+
placeholder="Subject: ...\nFrom: ...\nBody: ..."
|
| 69 |
+
)
|
| 70 |
+
submit = gr.Button("Analyze Email")
|
| 71 |
+
output_category = gr.Textbox(label="Category")
|
| 72 |
+
output_confidence = gr.Textbox(label="Confidence")
|
| 73 |
+
output_priority = gr.Textbox(label="Priority")
|
| 74 |
+
output_action = gr.Textbox(label="Suggested Action")
|
| 75 |
+
output_response = gr.Textbox(label="Draft Response")
|
| 76 |
+
output_items = gr.Textbox(label="Extracted Action Items")
|
| 77 |
+
submit.click(
|
| 78 |
+
email_assistant,
|
| 79 |
+
inputs=email_input,
|
| 80 |
+
outputs=[
|
| 81 |
+
output_category,
|
| 82 |
+
output_confidence,
|
| 83 |
+
output_priority,
|
| 84 |
+
output_action,
|
| 85 |
+
output_response,
|
| 86 |
+
output_items,
|
| 87 |
+
]
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
demo.launch()
|