Skip to content

Commit

Permalink
added timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
ombhojane committed Feb 11, 2024
1 parent c5d4ef2 commit cfe33e4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
Binary file modified __pycache__/app.cpython-311.pyc
Binary file not shown.
47 changes: 25 additions & 22 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
from flask import Flask, render_template, request
import google.generativeai as genai
import os
from concurrent.futures import ThreadPoolExecutor
import time
from concurrent.futures import ThreadPoolExecutor, as_completed

app = Flask(__name__)

API_KEY = os.getenv('GENAI_API_KEY')
genai.configure(api_key=API_KEY)

# Initialize a ThreadPoolExecutor for making asynchronous API calls
executor = ThreadPoolExecutor(max_workers=3)

# Placeholder function to simulate content generation for agrotourism services
# Define the timeout for waiting for the response
RESPONSE_TIMEOUT = 60 # in seconds, adjust as needed

def check_status(future, section):
try:
response = future.result(timeout=RESPONSE_TIMEOUT)
clean_response = response.text.replace("**", "")
return section, clean_response
except Exception as e:
return section, str(e) # Handle exceptions such as timeouts

def generate_description(service_name):
# Define prompt templates with specific sections
prompt_templates = {
"Business Model": f"Business Model Description for {service_name}:\nWhat is the business model of {service_name}? Please provide a concise description.",
"Setup Process": f"Setup Process for {service_name}:\nPlease describe the setup process for starting {service_name}, including necessary steps and key considerations.",
Expand All @@ -34,53 +43,47 @@ def generate_description(service_name):
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
]

model = genai.GenerativeModel(model_name="gemini-pro",
generation_config=generation_config,
safety_settings=safety_settings)

# Use a ThreadPoolExecutor to make asynchronous API calls
future_responses = {section: executor.submit(model.generate_content, [prompt])
for section, prompt in prompt_templates.items()}

# A dict to collect the final responses
responses = {}
for section, future in future_responses.items():
try:
response = future.result(timeout=10) # Set a timeout for each API call
clean_response = response.text.replace("**", "")
responses[section] = clean_response
except Exception as e:
responses[section] = str(e) # Handle exceptions such as timeouts

# Wait for the responses using as_completed
for future in as_completed(future_responses.values(), timeout=RESPONSE_TIMEOUT):
for section, future in future_responses.items():
if future.done():
section, result = check_status(future, section)
responses[section] = result

return responses


@app.route('/')
def index():
# Renders the homepage
return render_template('index.html')

@app.route('/predict')
def predict():
# Renders the prediction page
return render_template('predict.html')


@app.route('/generate', methods=['GET', 'POST'])
def generate():
if request.method == 'POST':
service_name = request.form['service_name']
response = generate_description(service_name)
# Ensure all threads have completed
executor.shutdown(wait=True)
return render_template('generate.html', response=response, service_name=service_name)
else:
return render_template('generate.html', response=None, service_name=None)


@app.route('/create')
def create():
# Renders the create page (if you have one)
return render_template('create.html')


if __name__ == '__main__':
app.run(debug=True)
return render_template('create.html')

0 comments on commit cfe33e4

Please sign in to comment.