Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,16 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import os
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
def generate_email(company, recipient_name, product):
|
| 8 |
-
api_key = os.environ.get("BASETEN_API_KEY")
|
| 9 |
if not api_key:
|
| 10 |
-
|
| 11 |
-
|
|
|
|
| 12 |
url = "https://model-4w56xy7q.api.baseten.co/development/predict"
|
| 13 |
|
| 14 |
headers = {
|
|
@@ -21,20 +23,50 @@ def generate_email(company, recipient_name, product):
|
|
| 21 |
"recipient_name": recipient_name,
|
| 22 |
"product_name": product
|
| 23 |
}
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
else:
|
| 33 |
-
|
| 34 |
|
| 35 |
# Create the Gradio interface
|
| 36 |
iface = gr.Interface(
|
| 37 |
-
fn=
|
| 38 |
inputs=[
|
| 39 |
gr.Textbox(label="Company Name"),
|
| 40 |
gr.Textbox(label="Recipient Name"),
|
|
@@ -42,8 +74,10 @@ iface = gr.Interface(
|
|
| 42 |
],
|
| 43 |
outputs=gr.Textbox(label="Generated Email"),
|
| 44 |
title="AI Email Generator",
|
| 45 |
-
description="Generate personalized emails using AI"
|
|
|
|
|
|
|
| 46 |
)
|
| 47 |
|
| 48 |
# Launch the interface
|
| 49 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
+
import time
|
| 5 |
+
import threading
|
| 6 |
+
import queue
|
| 7 |
|
| 8 |
+
def generate_email_async(company, recipient_name, product, progress=gr.Progress()):
|
| 9 |
+
api_key = os.environ.get("API_KEY")
|
|
|
|
|
|
|
| 10 |
if not api_key:
|
| 11 |
+
yield "Error: API_KEY not found in environment variables"
|
| 12 |
+
return
|
| 13 |
+
|
| 14 |
url = "https://model-4w56xy7q.api.baseten.co/development/predict"
|
| 15 |
|
| 16 |
headers = {
|
|
|
|
| 23 |
"recipient_name": recipient_name,
|
| 24 |
"product_name": product
|
| 25 |
}
|
| 26 |
+
|
| 27 |
+
yield "Initiating email generation..."
|
| 28 |
+
progress(0.1)
|
| 29 |
+
|
| 30 |
+
def make_request(result_queue):
|
| 31 |
+
try:
|
| 32 |
+
response = requests.post(url, headers=headers, json=data, timeout=300) # 5 minutes timeout
|
| 33 |
+
if response.status_code == 200:
|
| 34 |
+
result = response.json()
|
| 35 |
+
email_content = result['response'].split('[/INST]')[-1].strip()
|
| 36 |
+
result_queue.put(("success", email_content))
|
| 37 |
+
else:
|
| 38 |
+
result_queue.put(("error", f"Error: {response.status_code} - {response.text}"))
|
| 39 |
+
except requests.Timeout:
|
| 40 |
+
result_queue.put(("error", "Error: Request timed out. Please try again later."))
|
| 41 |
+
except Exception as e:
|
| 42 |
+
result_queue.put(("error", f"Error: An unexpected error occurred - {str(e)}"))
|
| 43 |
+
|
| 44 |
+
result_queue = queue.Queue()
|
| 45 |
+
thread = threading.Thread(target=make_request, args=(result_queue,))
|
| 46 |
+
thread.start()
|
| 47 |
+
|
| 48 |
+
start_time = time.time()
|
| 49 |
+
while thread.is_alive():
|
| 50 |
+
elapsed_time = time.time() - start_time
|
| 51 |
+
if elapsed_time > 300: # 5 minutes timeout
|
| 52 |
+
yield "Error: Process took too long. Please try again later."
|
| 53 |
+
return
|
| 54 |
+
yield f"Still working... (Elapsed time: {elapsed_time:.0f} seconds)"
|
| 55 |
+
progress(min(0.1 + (elapsed_time / 300) * 0.8, 0.9)) # Progress up to 90%
|
| 56 |
+
time.sleep(5) # Update every 5 seconds
|
| 57 |
+
|
| 58 |
+
thread.join()
|
| 59 |
+
status, result = result_queue.get()
|
| 60 |
+
|
| 61 |
+
if status == "success":
|
| 62 |
+
progress(1.0)
|
| 63 |
+
yield f"Email generated successfully:\n\n{result}"
|
| 64 |
else:
|
| 65 |
+
yield result
|
| 66 |
|
| 67 |
# Create the Gradio interface
|
| 68 |
iface = gr.Interface(
|
| 69 |
+
fn=generate_email_async,
|
| 70 |
inputs=[
|
| 71 |
gr.Textbox(label="Company Name"),
|
| 72 |
gr.Textbox(label="Recipient Name"),
|
|
|
|
| 74 |
],
|
| 75 |
outputs=gr.Textbox(label="Generated Email"),
|
| 76 |
title="AI Email Generator",
|
| 77 |
+
description="Generate personalized emails using AI (This process may take several minutes)",
|
| 78 |
+
allow_flagging="never",
|
| 79 |
+
live=True
|
| 80 |
)
|
| 81 |
|
| 82 |
# Launch the interface
|
| 83 |
+
iface.queue().launch()
|