File size: 2,107 Bytes
a89e051 974822f a89e051 3bb3cf2 974822f a89e051 3bb3cf2 a89e051 974822f 3bb3cf2 974822f 3bb3cf2 a89e051 3bb3cf2 a89e051 2891638 a89e051 3bb3cf2 a89e051 974822f a89e051 3bb3cf2 974822f 2891638 3bb3cf2 9b14e6f a89e051 974822f 3bb3cf2 2891638 3bb3cf2 2891638 3bb3cf2 a89e051 3bb3cf2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | import gradio as gr
import openai
import os
import pandas as pd
openai.api_key = os.getenv("OPENAI_API_KEY")
def generate_itinerary(prompt):
system_prompt = (
"You are a helpful travel assistant. Given a short travel request, "
"generate a detailed day-by-day itinerary in a clear format.\n\n"
"Requirements:\n"
"- Break down the trip by days (Day 1, Day 2, etc.)\n"
"- Include morning, afternoon, and evening suggestions\n"
"- Be specific but concise\n"
"- Focus on the traveler's stated interests\n"
)
user_prompt = f"Prompt: {prompt}\nItinerary:\n"
try:
response = openai.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
],
temperature=0.7,
max_tokens=700,
)
content = response.choices[0].message.content.strip()
# Convert newlines to <br> for proper table formatting
return content.replace("\n", "<br>")
except Exception as e:
return f"Error: {e}"
def batch_generate_itineraries(multi_prompt):
lines = [line.strip() for line in multi_prompt.splitlines() if line.strip()]
results = []
for req in lines:
itinerary = generate_itinerary(req)
results.append((req, itinerary))
return results # Returns list of (request, itinerary) tuples
iface = gr.Interface(
fn=batch_generate_itineraries,
inputs=gr.Textbox(
lines=10,
placeholder="Enter one travel request per line\nExample:\n3-day trip to Rome focused on history and food",
label="Multiple Travel Requests"
),
outputs=gr.Dataframe(
headers=["Request", "Generated Itinerary"],
datatype=["str", "html"],
label="Itineraries Table"
),
title="🧳 Batch Travel Itinerary Generator",
description="Paste multiple travel requests (one per line). Get structured GPT-4 itineraries in a table."
)
if __name__ == "__main__":
iface.launch() |