-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
210 additions
and
79 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,115 +1,183 @@ | ||
from flask import Flask, render_template, request | ||
from flask import Flask, render_template, request, session | ||
import google.generativeai as genai | ||
import os | ||
import time | ||
from concurrent.futures import ThreadPoolExecutor, as_completed | ||
# import torch | ||
# from PIL import Image | ||
# import numpy as np | ||
# from flask import jsonify, send_from_directory | ||
# from werkzeug.utils import secure_filename | ||
|
||
|
||
app = Flask(__name__) | ||
app.secret_key = os.urandom(24) # Necessary for session management | ||
|
||
API_KEY = os.getenv('GENAI_API_KEY') | ||
genai.configure(api_key=API_KEY) | ||
|
||
executor = ThreadPoolExecutor(max_workers=3) | ||
|
||
|
||
RESPONSE_TIMEOUT = 60 | ||
|
||
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) | ||
|
||
def generate_description(service_name): | ||
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.", | ||
"Budget Itinerary": f"Budget Itinerary for {service_name}:\nOutline a budget itinerary for {service_name}, detailing expected costs and financial planning advice." | ||
} | ||
def generate_description(prompt_parts, generation_config, safety_settings): | ||
model = genai.GenerativeModel(model_name="gemini-pro", | ||
generation_config=generation_config, | ||
safety_settings=safety_settings) | ||
response = model.generate_content(prompt_parts) | ||
clean_response = response.text.replace("**", "") | ||
return clean_response | ||
|
||
# Define generation config and safety settings (unchanged) | ||
generation_config = { | ||
def get_generation_config(): | ||
return { | ||
"temperature": 0.9, | ||
"top_p": 1, | ||
"top_k": 1, | ||
"max_output_tokens": 2048, | ||
} | ||
|
||
safety_settings = [ | ||
def get_safety_settings(): | ||
return [ | ||
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}, | ||
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}, | ||
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}, | ||
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}, | ||
] | ||
|
||
def format_response(response): | ||
# Convert markdown bullet points and bold text to HTML | ||
response = response.replace("**", "<strong>").replace("<strong>", "</strong>", 1) # Bold | ||
lines = response.split('\n') | ||
formatted_lines = [] | ||
for line in lines: | ||
if line.startswith('- ') or line.isdigit(): # Bullet points or numerical points | ||
formatted_lines.append(f"<li>{line[2:] if line.startswith('- ') else line}</li>") | ||
else: | ||
formatted_lines.append(line) | ||
formatted_response = "<ul>" + "\n".join(formatted_lines) + "</ul>" if formatted_lines else response | ||
return formatted_response.replace("<ul></ul>", "") # Remove empty list tags | ||
|
||
# adding visualiztions | ||
|
||
# device = "cuda" if torch.cuda.is_available() else "cpu" | ||
|
||
# UPLOAD_FOLDER = 'uploads' | ||
# GENERATED_FOLDER = 'generated' | ||
# app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER | ||
# app.config['GENERATED_FOLDER'] = GENERATED_FOLDER | ||
|
||
# os.makedirs(UPLOAD_FOLDER, exist_ok=True) | ||
# os.makedirs(GENERATED_FOLDER, exist_ok=True) | ||
|
||
# def generate_image_with_ml_model(image_path, prompt): | ||
# # Load the image | ||
# input_image = load_image(image_path).to(device) | ||
|
||
# # Initialize the depth estimator | ||
# depth_estimator = pipeline("depth-estimation", device=device) | ||
|
||
model = genai.GenerativeModel(model_name="gemini-pro", | ||
generation_config=generation_config, | ||
safety_settings=safety_settings) | ||
|
||
future_responses = {section: executor.submit(model.generate_content, [prompt]) | ||
for section, prompt in prompt_templates.items()} | ||
|
||
# A dict to collect the final responses | ||
responses = {} | ||
# # Process to obtain depth map | ||
# depth_map = get_depth_map(input_image, depth_estimator) # Assuming get_depth_map is defined similarly to your Colab code | ||
|
||
# # Initialize the ControlNet model and pipeline | ||
# controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-normal", torch_dtype=torch.float16, use_safetensors=True).to(device) | ||
# pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained( | ||
# "runwayml/stable-diffusion-v1-5", | ||
# controlnet=controlnet, | ||
# torch_dtype=torch.float16, | ||
# use_safetensors=True | ||
# ).to(device) | ||
# pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config) | ||
# pipe.enable_model_cpu_offload() | ||
|
||
# # Generate the image | ||
# output = pipe(prompt=prompt, image=input_image, control_image=depth_map).images[0] | ||
|
||
# # Convert tensor to PIL Image for saving | ||
# output_image = Image.fromarray(output.mul(255).clamp(0, 255).byte().cpu().numpy().astype(np.uint8).transpose(1, 2, 0)) | ||
|
||
try: | ||
for future in as_completed(future_responses.values(), timeout=RESPONSE_TIMEOUT): | ||
section = None | ||
for sec, fut in future_responses.items(): | ||
if fut == future: | ||
section = sec | ||
break | ||
if section: | ||
try: | ||
response = future.result() # We already have a timeout in as_completed | ||
clean_response = response.text.replace("**", "") | ||
responses[section] = clean_response | ||
except TimeoutError: | ||
responses[section] = "Timeout occurred while generating content" | ||
except Exception as e: | ||
responses[section] = f"An error occurred: {e}" | ||
finally: | ||
# Cancel any pending futures if they are not done | ||
for future in future_responses.values(): | ||
if not future.done(): | ||
future.cancel() | ||
|
||
# Shutdown the executor in a clean way | ||
executor.shutdown(wait=False) | ||
|
||
return responses | ||
# return output_image | ||
|
||
# @app.route('/generate-image', methods=['POST']) | ||
# def generate_image_endpoint(): | ||
# if 'image' not in request.files: | ||
# return jsonify({'error': 'No image part'}), 400 | ||
# file = request.files['image'] | ||
# prompt = request.form.get('prompt', '') # Get the prompt from the form data | ||
# if file.filename == '': | ||
# return jsonify({'error': 'No selected file'}), 400 | ||
# if file and prompt: | ||
# filename = secure_filename(file.filename) | ||
# input_filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) | ||
# file.save(input_filepath) | ||
|
||
# # Generate the image | ||
# output_image = generate_image_with_ml_model(input_filepath, prompt) | ||
# output_filename = f"generated_{filename}" | ||
# output_filepath = os.path.join(app.config['GENERATED_FOLDER'], output_filename) | ||
# output_image.save(output_filepath) | ||
|
||
# return jsonify({'generatedImageUrl': f'/generated/{output_filename}'}) | ||
# else: | ||
# return jsonify({'error': 'Invalid request'}), 400 | ||
|
||
# @app.route('/generated/<filename>') | ||
# def generated_image(filename): | ||
# return send_from_directory(app.config['GENERATED_FOLDER'], filename) | ||
|
||
|
||
@app.route('/visualize') | ||
def visualize(): | ||
return render_template('visualize.html') | ||
|
||
@app.route('/') | ||
def index(): | ||
session.clear() # Clear session at the start | ||
return render_template('index.html') | ||
|
||
@app.route('/predict') | ||
def predict(): | ||
# Renders the prediction page | ||
def pedict(): | ||
session.clear() # Clear session at the start | ||
return render_template('predict.html') | ||
|
||
@app.route('/generate', methods=['GET', 'POST']) | ||
def generate(): | ||
if request.method == 'POST': | ||
service_name = request.form['service_name'] | ||
try: | ||
response = generate_description(service_name) | ||
return render_template('generate.html', response=response, service_name=service_name) | ||
except TimeoutError as e: | ||
# Log the error here | ||
print(f"A timeout occurred: {e}") | ||
# Return a message to the user or redirect to an error page | ||
return render_template('error.html', message="The server took too long to respond.") | ||
service_name = session.get('service_name', request.form['service_name']) | ||
session['service_name'] = service_name | ||
|
||
sections = session.get('sections', []) | ||
current_section = request.form.get('section', 'description') | ||
|
||
is_final_section = False # Flag to indicate if the current section is the final one | ||
|
||
if 'accept' in request.form: | ||
next_section_map = { | ||
'description': 'business_model', | ||
'business_model': 'setup_process', | ||
'setup_process': 'budget', | ||
'budget': 'end' # Indicates the end of the sections | ||
} | ||
next_section = next_section_map.get(current_section) | ||
|
||
if next_section == 'end': | ||
is_final_section = True # Set the flag when we reach the final section | ||
elif next_section: | ||
sections.append({'name': next_section, 'content': ''}) | ||
current_section = next_section | ||
elif sections and sections[-1]['name'] == current_section: | ||
pass | ||
else: | ||
sections.append({'name': current_section, 'content': ''}) | ||
|
||
prompt_parts = [f"Generate {current_section} for {service_name} in the context of agrotourism."] | ||
|
||
response = generate_description(prompt_parts, get_generation_config(), get_safety_settings()) | ||
formatted_response = format_response(response) # Apply formatting to the response | ||
sections[-1]['content'] = formatted_response | ||
|
||
session['sections'] = sections | ||
|
||
return render_template('generate.html', sections=sections, service_name=service_name, is_final_section=is_final_section) | ||
else: | ||
return render_template('generate.html', response=None, service_name=None) | ||
session.pop('sections', None) # Clear session for new start | ||
return render_template('generate.html', sections=[], service_name=None, is_final_section=False) | ||
|
||
|
||
@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) | ||
app.run(debug=True) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Chalo Kisaan Agrotourism Planner</title> | ||
<script src="https://cdn.tailwindcss.com"></script> | ||
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600&display=swap" rel="stylesheet"> | ||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"> | ||
</head> | ||
<body class="bg-gray-50 dark:bg-gray-900 text-gray-900 dark:text-gray-200"> | ||
<!-- | ||
// v0 by Vercel. | ||
// https://v0.dev/t/Fsu9Mcbm2yR | ||
--> | ||
<div class="flex w-full min-h-screen flex-col"><header class="flex h-20 w-full shrink-0 items-center border-b px-4 md:px-6"><a class="flex items-center space-x-2 text-lg font-semibold" href="#"> | ||
Acme Inc | ||
</a></header><div class="flex flex-1 w-full max-w-7xl mx-auto overflow-hidden min-h-0"><div class="grid w-[250px] border-r overflow-auto md:w-[300px]"><div class="grid gap-2 p-4"><form class="relative"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="absolute left-2.5 top-2.5 h-4 w-4 text-gray-500 dark:text-gray-400"><circle cx="11" cy="11" r="8"></circle><path d="m21 21-4.3-4.3"></path></svg><input class="flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 pl-8" placeholder="Search the docs (t or /)" type="search"></form><div class="space-y-2"><a class="flex items-center font-medium rounded-md px-3 py-2 transition-colors hover:bg-gray-100 hover:text-gray-900 dark:hover:bg-gray-800 dark:hover:text-gray-50" href="#"> | ||
Getting Started | ||
</a><div class="grid ml-4 space-y-2"><a class="flex items-center font-medium rounded-md px-3 py-2 transition-colors hover:bg-gray-100 hover:text-gray-900 dark:hover:bg-gray-800 dark:hover:text-gray-50" href="#"> | ||
Installation | ||
</a><a class="flex items-center font-medium rounded-md px-3 py-2 transition-colors hover:bg-gray-100 hover:text-gray-900 dark:hover:bg-gray-800 dark:hover:text-gray-50" href="#"> | ||
Configuration | ||
</a><a class="flex items-center font-medium rounded-md bg-gray-100 px-3 py-2 transition-colors hover:bg-gray-100 hover:text-gray-900 dark:bg-gray-800 dark:hover:bg-gray-800 dark:hover:text-gray-50" href="#"> | ||
Usage | ||
</a><a class="flex items-center font-medium rounded-md px-3 py-2 transition-colors hover:bg-gray-100 hover:text-gray-900 dark:hover:bg-gray-800 dark:hover:text-gray-50" href="#"> | ||
API Reference | ||
</a></div><a class="flex items-center font-medium rounded-md px-3 py-2 transition-colors hover:bg-gray-100 hover:text-gray-900 dark:hover:bg-gray-800 dark:hover:text-gray-50" href="#"> | ||
Deployment | ||
</a><a class="flex items-center font-medium rounded-md px-3 py-2 transition-colors hover:bg-gray-100 hover:text-gray-900 dark:hover:bg-gray-800 dark:hover:text-gray-50" href="#"> | ||
Customization | ||
</a></div><a class="flex items-center font-medium rounded-md px-3 py-2 transition-colors hover:bg-gray-100 hover:text-gray-900 dark:hover:bg-gray-800 dark:hover:text-gray-50" href="#"> | ||
Configuration | ||
</a><a class="flex items-center font-medium rounded-md px-3 py-2 transition-colors hover:bg-gray-100 hover:text-gray-900 dark:hover:bg-gray-800 dark:hover:text-gray-50" href="#"> | ||
Usage | ||
</a><a class="flex items-center font-medium rounded-md px-3 py-2 transition-colors hover:bg-gray-100 hover:text-gray-900 dark:hover:bg-gray-800 dark:hover:text-gray-50" href="#"> | ||
Deployment | ||
</a><a class="flex items-center font-medium rounded-md px-3 py-2 transition-colors hover:bg-gray-100 hover:text-gray-900 dark:hover:bg-gray-800 dark:hover:text-gray-50" href="#"> | ||
Customization | ||
</a></div></div><div class="flex-1 overflow-auto py-8 px-6 space-y-4 text-gray-900 dark:text-gray-300"><h1 class="text-3xl font-extrabold tracking-tight"> | ||
Getting Started | ||
</h1><div class="prose max-w-none w-[700px] grid gap-4 [&>h1]:first:mt-4"><p> | ||
Welcome to the getting started guide. We'll walk you through | ||
the basics of using our product. | ||
</p><h2>Installation</h2><p> | ||
To get started, you'll need to install our CLI tool. You can | ||
do this using npm. | ||
</p><pre><code>$ npm install -g my-product-cli</code></pre><h2>Usage</h2><p> | ||
Once you have the CLI tool installed, you can use it to create a | ||
new project. | ||
</p><pre><code>$ my-product create my-project</code></pre><h2>Deployment</h2><p> | ||
When you're ready to deploy your project, simply run the | ||
deploy command. | ||
</p><pre><code>$ my-product deploy</code></pre><h2>Customization</h2><p> | ||
You can customize the look and feel of your project by editing the | ||
configuration file. | ||
</p><ul><li>Open the config file.</li><li>Make your changes.</li><li>Save the file.</li></ul></div></div></div></div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters