Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678910111213141521 unmodified lines37383940414219 unmodified lines6263646566676836 unmodified lines10510610710810911011137 unmodified lines1491501511521531542 unmodified lines1571581591601611621632 unmodified lines166167168169import osimport gradio as grfrom groq import Groqfrom reportlab.pdfgen import canvasfrom reportlab.lib.pagesizes import letterimport tempfilefrom textwrap import wrapimport tempfileimport refrom reportlab.platypus import SimpleDocTemplate, Paragraph, Spacerfrom reportlab.lib.styles import getSampleStyleSheetfrom reportlab.lib.pagesizes import letter# Load API key securely from environmentGROQ_API_KEY = os.getenv("GROQ_API_KEY")21 unmodified lines"Any": "images/default.png"}def generate_itinerary(mood, location, budget, days):mood_text = "any mood" if mood == "Any" else moodbudget_text = "any budget" if budget == "Any" else budget19 unmodified linesitinerary_text = completion.choices[0].message.contentimage_path = MOOD_IMAGES.get(mood, MOOD_IMAGES["Any"])return itinerary_text, image_path, gr.update(visible=True)def generate_pdf(itinerary_text):if not itinerary_text.strip():36 unmodified linesreturn temp_pdf.namewith gr.Blocks(theme=gr.themes.Default(primary_hue="teal", secondary_hue="teal")) as demo:# Header with gr.HTML for styles and animationheader_html = """37 unmodified lineswith gr.Column():itinerary_output = gr.Markdown(label="Generated Itinerary")image_output = gr.Image(label="Travel Mood Image", type="filepath", visible=False)with gr.Column():download_btn = gr.Button("📥 Download Itinerary as PDF", variant="secondary")2 unmodified linesgenerate_btn.click(generate_itinerary,inputs=[mood, location, budget, days],outputs=[itinerary_output, image_output, image_output])download_btn.click(2 unmodified linesoutputs=pdf_file)demo.launch()1234567891011121314151617181920212221 unmodified lines444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899019 unmodified lines11011111211311411511611711811912036 unmodified lines15715815916016116237 unmodified lines2002012022032042052062 unmodified lines2092102112122132142152 unmodified lines218219220221import spacy.cli# Download the model if it's not already presentspacy.cli.download("en_core_web_sm")import spacy# Now load the model as usualnlp = spacy.load("en_core_web_sm")import osimport gradio as grfrom groq import Groqfrom reportlab.pdfgen import canvasfrom reportlab.lib.pagesizes import letterimport tempfileimport refrom reportlab.platypus import SimpleDocTemplate, Paragraph, Spacerfrom reportlab.lib.styles import getSampleStyleSheetfrom reportlab.lib.pagesizes import letterfrom geopy.geocoders import Nominatimimport foliumfrom IPython.display import display# Load API key securely from environmentGROQ_API_KEY = os.getenv("GROQ_API_KEY")21 unmodified lines"Any": "images/default.png"}# Load SpaCy model once globallynlp = spacy.load("en_core_web_sm")def clean_place_name(name):import rereturn re.sub(r'^(the|a|an)\s+', '', name, flags=re.IGNORECASE).strip()def extract_places_and_map(itinerary_text, city_context=None):# Extract places using regex + NERpattern = r"(visit|explore|trip to|at|go to|see)\s+([A-Z][A-Za-zÀ-ÖØ-öø-ÿ'’\- ]+)"matches = re.findall(pattern, itinerary_text, re.IGNORECASE)rule_places = [m[1].strip() for m in matches]doc = nlp(itinerary_text)ner_places = [ent.text for ent in doc.ents if ent.label_ in ["GPE", "LOC", "FAC"]]places = list(set(rule_places + ner_places))geolocator = Nominatim(user_agent="feelaway_app")locations = []for place in places:try:query = f"{place}, {city_context}" if city_context else placeloc = geolocator.geocode(query, language='en', timeout=10)if loc:locations.append((place, loc.latitude, loc.longitude))except Exception as e:# Skip failed geocoding quietlycontinueif not locations:return "<p>No locations found for mapping.</p>"# Create Folium map centered on first locationm = folium.Map(location=[locations[0][1], locations[0][2]], zoom_start=10)for name, lat, lon in locations:folium.Marker([lat, lon], popup=name).add_to(m)# Render map HTML stringreturn m.get_root().render()def generate_itinerary(mood, location, budget, days):mood_text = "any mood" if mood == "Any" else moodbudget_text = "any budget" if budget == "Any" else budget19 unmodified linesitinerary_text = completion.choices[0].message.contentimage_path = MOOD_IMAGES.get(mood, MOOD_IMAGES["Any"])# Generate map HTML from itinerary text and user locationmap_html = extract_places_and_map(itinerary_text, city_context=location_text if location_text.lower() != "any destination" else None)return itinerary_text, image_path, gr.update(visible=True), map_htmldef generate_pdf(itinerary_text):if not itinerary_text.strip():36 unmodified linesreturn temp_pdf.namewith gr.Blocks(theme=gr.themes.Default(primary_hue="teal", secondary_hue="teal")) as demo:# Header with gr.HTML for styles and animationheader_html = """37 unmodified lineswith gr.Column():itinerary_output = gr.Markdown(label="Generated Itinerary")image_output = gr.Image(label="Travel Mood Image", type="filepath", visible=False)map_output = gr.HTML(label="Map of Suggested Places")with gr.Column():download_btn = gr.Button("📥 Download Itinerary as PDF", variant="secondary")2 unmodified linesgenerate_btn.click(generate_itinerary,inputs=[mood, location, budget, days],outputs=[itinerary_output, image_output, image_output, map_output])download_btn.click(2 unmodified linesoutputs=pdf_file)demo.launch()
app.py+58-6