Spaces:
Build error

PlanItAI / app.py
derangedsaturnian's picture
Update app.py
8c2a9fe verified
Raw History Blame Contribute Delete
3.71 kB
from dotenv import load_dotenv,find_dotenv
import gradio as gr
#import openai
import google.generativeai as genai
import os
import time
from markdown import markdown
import re
load_dotenv(find_dotenv())
#openai.api_key = os.getenv("OPENAI_API_KEY")
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
conversation_history=[]
def format_response(markdown_txt):
html_txt=markdown(markdown_txt)
return re.sub(r'<[^>]*>','',html_txt)
def generate_trip_plan(user_input):
system_instruction = """
You are Luka, a friendly, polite, extroverted, funny travel assistant.
You help users plan their trip by asking follow-up questions and providing personalized recommendations based on their responses.
Ask the users about budget, purpose, duration, and preferences one by one after the users provide the destination they want to travel to.
For example: After the user provides the destination, ask about the budget, then about the duration, and so on in a step-by-step manner.
Use the conversation history to maintain context and guide the user effectively.
After gathering the above details, ask follow-up questions like dietary preferences, walking tolerance, accommodation preferences, etc., step by step for (e.g. Ask bout the user's dietary preferences, then walking tolerance or commute style and so on...)
Incase the user input has typo for e.g. for destination they type 'Autra' instead of Australia then you provide them suggestions with places starting from Au like Australia, Austria etc., analyze the input and provide suggestions for valid destinations based on similar spelling or context.
If the user input is vague and unclear ask clarifying quetsions before concluding.
Recommend places and activities based on their input and specified destination.
Keep the conversation natural and human-like. Only end the conversation if the user says 'thank you', or conversation ending phrases.
"""
conversation_history.append({"role": "user","parts": [user_input]})
messages=[{"role": "model","parts": [system_instruction]}]+conversation_history
#messages=[
# {"role": "model","content": system_message},
## {"role": "user","content": prompt}
# ]
#response=openai.completions.create(
# model="gemini-1.5-pro",
# prompt=prompt,
# temperature=0.5,
# max_tokens=500
#)
generation_config={
"temperature": 0.7,
"max_output_tokens": 5000,
"response_mime_type": "text/plain",
}
model=genai.GenerativeModel(
model_name="gemini-1.5-pro",
generation_config=generation_config,
)
chat_session=model.start_chat(history=messages)
try:
time.sleep(1)
response=chat_session.send_message(user_input)
print("Response:",response.text)
recommendations=response.text.strip()
plain_text_recommendations=format_response(recommendations)
conversation_history.append({"role": "assistant","parts": [plain_text_recommendations]})
return plain_text_recommendations
except Exception as e:
print("Error:",e)
if "429"in str(e):
return f"Luka is asleep(API quota exhausted)"
else:
return f"Luka is not available at the moment. Please try again later"
demo = gr.Interface(
fn=generate_trip_plan,
inputs=gr.Textbox(label="Your Input", placeholder="Say something to Luka..."),
outputs=gr.Textbox(label="Luka's response"),
title="Travel Buddy Luka",
description="Chat with Luka to plan your perfect trip! Enter your destination as a first step.",
)
demo.launch(share=True)