SatyamPrakash09 commited on
Commit
32fdf3e
·
verified ·
1 Parent(s): dac55d9

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """TravellerBuddy.ipynb
2
+
3
+ Shareable file is located at
4
+ https://colab.research.google.com/drive/1kxJ6352BrFxdCTnQqN9maEPkGMI1FESy?usp=sharing
5
+
6
+ # Gen AI
7
+
8
+ ---
9
+ BY: SATYAM PRAKASH
10
+ ---
11
+ """
12
+
13
+ # !pip install langchain
14
+ # !pip install openai
15
+ # !pip install gradio
16
+ # !pip install huggingface_hub
17
+ # !pip install langchain_community
18
+ # !pip install langchain-google-genai
19
+
20
+ import os
21
+ import gradio as gr
22
+ from langchain.chat_models import ChatOpenAI
23
+ from langchain import LLMChain, PromptTemplate
24
+ from langchain.memory import ConversationBufferMemory
25
+ from langchain_google_genai import ChatGoogleGenerativeAI
26
+ import os
27
+ from dotenv import load_dotenv
28
+
29
+ load_dotenv()
30
+ os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")
31
+
32
+
33
+ from langchain_google_genai import ChatGoogleGenerativeAI
34
+
35
+ # Try creating a Gemini Flash 2.0 model
36
+ try:
37
+ llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash", temperature=0)
38
+ response = llm.invoke("Hello Gemini, can you hear me?")
39
+ print("✅ API is working!")
40
+ print("Response:", response.content)
41
+ except Exception as e:
42
+ print("❌ API Error:", str(e))
43
+
44
+ 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
45
+ {chat_history}
46
+ User: {user_message}
47
+ Chatbot:"""
48
+
49
+ prompt = PromptTemplate(
50
+ input_variables=["chat_history", "user_message"], template=template
51
+ )
52
+
53
+
54
+ from langchain.memory import ConversationBufferMemory
55
+ memory = ConversationBufferMemory(memory_key="chat_history")
56
+
57
+ llm_chain = LLMChain(
58
+ llm=llm,
59
+ prompt=prompt,
60
+ memory=memory
61
+ )
62
+
63
+ def get_text_response(user_message, history):
64
+ # LangChain memory handles the history internally
65
+ response = llm_chain.predict(user_message=user_message)
66
+ return response
67
+
68
+ # # Gradio Chat Interface
69
+ # demo = gr.ChatInterface(
70
+ # fn=get_text_response,
71
+ # examples=[
72
+ # "How are you doing?",
73
+ # "What are your interests?",
74
+ # "Which places do you like to visit?"
75
+ # ]
76
+ # )
77
+
78
+ demo = gr.ChatInterface(get_text_response, examples=["How are you doing?","What are your interests?","Which places do you like to visit?"], type='messages')
79
+
80
+ if __name__ == "__main__":
81
+ demo.launch() #To create a public link, set `share=True` in `launch()`. To enable errors and logs, set `debug=True` in `launch()`.