Spaces:
Sleeping
Sleeping
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536372 unmodified lines4041424344454647484950515253545556575859606162636465666768697071727374757677787980"""TravellerBuddy.ipynbShareable file is located athttps://colab.research.google.com/drive/1kxJ6352BrFxdCTnQqN9maEPkGMI1FESy?usp=sharing# Gen AI---BY: SATYAM PRAKASH---"""# !pip install langchain# !pip install openai# !pip install gradio# !pip install huggingface_hub# !pip install langchain_community# !pip install langchain-google-genaiimport osimport gradio as grfrom langchain import LLMChain, PromptTemplatefrom langchain.memory import ConversationBufferMemoryfrom langchain_google_genai import ChatGoogleGenerativeAIimport osfrom dotenv import load_dotenvfrom langchain_google_genai import ChatGoogleGenerativeAI# load_dotenv()os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")# Try creating a Gemini Flash 2.0 modeltry:llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash", temperature=0)response = llm.invoke("Hello Gemini, can you hear me?")2 unmodified linesexcept Exception as e:print("❌ API Error:", str(e))template = """As an adventurous and globetrotting college student, you're constantly on the lookout for new cultures, experiences, and breathtaking landscapes. You've visited numerous countries, immersing yourself in local traditions, and you're always eager to swap travel stories and offer tips on exciting destinations{chat_history}User: {user_message}Chatbot:"""prompt = PromptTemplate(input_variables=["chat_history", "user_message"], template=template)from langchain.memory import ConversationBufferMemorymemory = ConversationBufferMemory(memory_key="chat_history")llm_chain = LLMChain(llm=llm,prompt=prompt,memory=memory)def get_text_response(user_message, history):# LangChain memory handles the history internallyresponse = llm_chain.predict(user_message=user_message)return response# # Gradio Chat Interface# demo = gr.ChatInterface(# fn=get_text_response,# examples=[# "How are you doing?",# "What are your interests?",# "Which places do you like to visit?"# ]# )demo = gr.ChatInterface(get_text_response, examples=["How are you doing?","What are your interests?","Which places do you like to visit?"], type='messages')if __name__ == "__main__":demo.launch() #To create a public link, set `share=True` in `launch()`. To enable errors and logs, set `debug=True` in `launch()`.1234567891011121314151617181920212 unmodified lines242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768# Install dependencies (Hugging Face will run this in requirements.txt ideally)# !pip install langchain langchain-community langchain-google-genai gradio huggingface_hub python-dotenvimport osimport gradio as grfrom dotenv import load_dotenv# Updated imports for latest LangChain (Aug 2025)from langchain.chains import LLMChainfrom langchain_core.prompts import PromptTemplatefrom langchain.memory import ConversationBufferMemoryfrom langchain_google_genai import ChatGoogleGenerativeAI# Load API keys from environmentload_dotenv()os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")# Try creating a Gemini Flash 2.5 modeltry:llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash", temperature=0)response = llm.invoke("Hello Gemini, can you hear me?")2 unmodified linesexcept Exception as e:print("❌ API Error:", str(e))# Prompt Templatetemplate = """As an adventurous and globetrotting college student,you're constantly on the lookout for new cultures, experiences, and breathtaking landscapes.You've visited numerous countries, immersing yourself in local traditions, and you're always eager to swap travel storiesand offer tips on exciting destinations.{chat_history}User: {user_message}Chatbot:"""prompt = PromptTemplate(input_variables=["chat_history", "user_message"],template=template)# Memory to store chat historymemory = ConversationBufferMemory(memory_key="chat_history")# LLM Chainllm_chain = LLMChain(llm=llm,prompt=prompt,memory=memory)# Function for Gradiodef get_text_response(user_message, history):return llm_chain.predict(user_message=user_message)# Gradio Chat Interfacedemo = gr.ChatInterface(fn=get_text_response,examples=["How are you doing?","What are your interests?","Which places do you like to visit?"],type="messages")if __name__ == "__main__":demo.launch()
app.py+34-46