Skip to content

Commit

Permalink
MAJOR Changes! backend improved of generate
Browse files Browse the repository at this point in the history
  • Loading branch information
ombhojane committed Feb 14, 2024
1 parent c871c5d commit 6ba41a8
Show file tree
Hide file tree
Showing 4 changed files with 396 additions and 162 deletions.
140 changes: 69 additions & 71 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ def format_response(response):
formatted_response = "<ul>" + "\n".join(formatted_lines) + "</ul>" if formatted_lines else response
return formatted_response.replace("<ul></ul>", "") # Remove empty list tags

def calculate_progress(sections):
completed_sections = [section for section in sections if section['content']]
total_sections = 4 # description, business_model, setup_process, budget
progress = int((len(completed_sections) / total_sections) * 100)
return progress


# adding visualiztions

Expand Down Expand Up @@ -145,88 +139,92 @@ def pedict():
session.clear() # Clear session at the start
return render_template('predict.html')

@app.route('/download', methods=['POST'])
def download():
service_name = request.form['service_name']
approved_sections = session.get('approved_sections', {})
# You need to implement the logic to convert approved_sections into a downloadable format
# For example:
download_content = "Your Agrotourism Service Plan\n"
for section_name, section_content in approved_sections.items():
download_content += f"{section_name.title()}\n{section_content}\n\n"

# Create a response with the content
response = make_response(download_content)
response.headers["Content-Disposition"] = "attachment; filename=service_plan.txt"
response.headers["Content-Type"] = "text/plain"
return response

@app.route('/generate', methods=['GET', 'POST'])
def generate():
# Initialize or get current section and form data from session
if request.method == 'POST':
service_name = session.get('service_name', request.form['service_name'])
session['service_name'] = service_name

# Ensure form_data is updated or initialized correctly
form_data = {
'service_name': request.form['service_name'],
'land_size': request.form.get('land_size', 'N/A'),
'biodiversity': request.form.get('biodiversity', 'N/A'),
'budget': request.form.get('budget', 'N/A'),
'infrastructure': request.form.getlist('infrastructure[]'),
}
session['form_data'] = form_data

# Initialize or get sections from session
sections = session.get('sections', [])
approved_sections = session.get('approved_sections', {})
current_section = request.form.get('section', 'description')
is_final_section = False
if sections:
# Determine the current section from the last entry in sections
current_section = sections[-1]['name']
else:
# Default to starting with 'description'
current_section = 'description'

# Determine next section
section_order = ['description', 'business_model', 'setup_process', 'budget']
current_index = section_order.index(current_section)

if 'accept' in request.form:
# Store the current approved section content
approved_sections[current_section] = sections[-1]['content']
session['approved_sections'] = approved_sections

next_section_map = {
'description': 'business_model',
'business_model': 'setup_process',
'setup_process': 'budget',
'budget': 'end'
}
next_section = next_section_map.get(current_section)

if next_section == 'end':
is_final_section = True
# Prepare for download, show download button
progress = 100 # Final section implies 100% progress
# Render the template with the final section and show the download button
return render_template('generate.html', sections=sections, service_name=service_name,
is_final_section=is_final_section, progress=progress,
approved_sections=approved_sections, show_download=True)
# If accepting current section, move to next unless at the end
if current_index + 1 < len(section_order):
current_section = section_order[current_index + 1]
else:
# Append the new section if it's not the end
sections.append({'name': next_section, 'content': ''})
current_section = next_section

# If at the last section, perhaps redirect or indicate completion
return render_template('complete.html', sections=sections, service_name=form_data['service_name'])
elif 'regenerate' in request.form:
# If regenerate is clicked, simply re-display the current section with the old content
# There's no need to append a new section or generate new content
pass

# If regenerating, stay on current_section
pass # current_section remains the same
else:
# New section is being started without any user approval action
sections.append({'name': current_section, 'content': ''})
# Handling for generating the first section or when not accepting/regenerating
if not sections: # If starting fresh
current_section = 'description'

# Generate content for the current section
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())
# Generate content for the current or next section
prompt_template = create_prompt_template(current_section, form_data)
response = generate_description(prompt_template, get_generation_config(), get_safety_settings())
formatted_response = format_response(response)
sections[-1]['content'] = formatted_response

# Update sections list appropriately
if 'accept' in request.form and sections:
# Replace last section content if regenerating; otherwise, append new section
sections[-1] = {'name': current_section, 'content': formatted_response}
else:
sections.append({'name': current_section, 'content': formatted_response})

# Save updated sections and form_data back to session
session['sections'] = sections
progress = calculate_progress(sections)

return render_template('generate.html', sections=sections, service_name=service_name,
is_final_section=is_final_section, progress=progress,
approved_sections=approved_sections, show_download=False)
# Calculate progress for UI feedback
progress = calculate_progress(sections, section_order)

# Render the template with updated context
return render_template('generate.html', sections=sections, current_section=current_section, service_name=form_data['service_name'], progress=progress)
else:
# Clear the session for a new start
session.pop('sections', None)
session.pop('approved_sections', None)
return render_template('generate.html', sections=[], service_name=None,
is_final_section=False, progress=0, approved_sections={},
show_download=False)
# Clear the session for a new start and render the initial form
session.clear()
return render_template('generate.html', sections=[], current_section='description', service_name='', progress=0)

def calculate_progress(sections, section_order):
# Calculate the progress based on sections completed
completed_sections = len(sections)
total_sections = len(section_order)
progress = int((completed_sections / total_sections) * 100)
return progress

def create_prompt_template(current_section, form_data):
# Create a detailed prompt for the current section based on form_data
# Adjust this function as necessary to tailor prompts for each section
prompt_parts = [
f"Generate {current_section} for {form_data['service_name']}",
f"Land Size: {form_data['land_size']} hectares",
f"Biodiversity: {form_data['biodiversity']}",
f"Budget: INR {form_data['budget']}",
f"Existing Infrastructure: {', '.join(form_data['infrastructure'])}.",
]
return " ".join(prompt_parts)


if __name__ == '__main__':
Expand Down
Loading

0 comments on commit 6ba41a8

Please sign in to comment.