Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,21 @@
|
|
| 1 |
-
|
| 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 import LLMChain, PromptTemplate
|
| 23 |
-
from langchain.memory import ConversationBufferMemory
|
| 24 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 25 |
-
import os
|
| 26 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 28 |
|
| 29 |
-
#
|
|
|
|
| 30 |
os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
# Try creating a Gemini Flash 2.0 model
|
| 35 |
try:
|
| 36 |
llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash", temperature=0)
|
| 37 |
response = llm.invoke("Hello Gemini, can you hear me?")
|
|
@@ -40,41 +24,45 @@ try:
|
|
| 40 |
except Exception as e:
|
| 41 |
print("❌ API Error:", str(e))
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
{chat_history}
|
| 45 |
User: {user_message}
|
| 46 |
Chatbot:"""
|
| 47 |
|
| 48 |
prompt = PromptTemplate(
|
| 49 |
-
input_variables=["chat_history", "user_message"],
|
|
|
|
| 50 |
)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
from langchain.memory import ConversationBufferMemory
|
| 54 |
memory = ConversationBufferMemory(memory_key="chat_history")
|
| 55 |
|
|
|
|
| 56 |
llm_chain = LLMChain(
|
| 57 |
llm=llm,
|
| 58 |
prompt=prompt,
|
| 59 |
memory=memory
|
| 60 |
)
|
| 61 |
|
|
|
|
| 62 |
def get_text_response(user_message, history):
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
# )
|
| 76 |
-
|
| 77 |
-
demo = gr.ChatInterface(get_text_response, examples=["How are you doing?","What are your interests?","Which places do you like to visit?"], type='messages')
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|
| 80 |
-
demo.launch()
|
|
|
|
| 1 |
+
# Install dependencies (Hugging Face will run this in requirements.txt ideally)
|
| 2 |
+
# !pip install langchain langchain-community langchain-google-genai gradio huggingface_hub python-dotenv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
import os
|
| 5 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
+
|
| 8 |
+
# Updated imports for latest LangChain (Aug 2025)
|
| 9 |
+
from langchain.chains import LLMChain
|
| 10 |
+
from langchain_core.prompts import PromptTemplate
|
| 11 |
+
from langchain.memory import ConversationBufferMemory
|
| 12 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 13 |
|
| 14 |
+
# Load API keys from environment
|
| 15 |
+
load_dotenv()
|
| 16 |
os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")
|
| 17 |
|
| 18 |
+
# Try creating a Gemini Flash 2.5 model
|
|
|
|
|
|
|
| 19 |
try:
|
| 20 |
llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash", temperature=0)
|
| 21 |
response = llm.invoke("Hello Gemini, can you hear me?")
|
|
|
|
| 24 |
except Exception as e:
|
| 25 |
print("❌ API Error:", str(e))
|
| 26 |
|
| 27 |
+
# Prompt Template
|
| 28 |
+
template = """As an adventurous and globetrotting college student,
|
| 29 |
+
you're constantly on the lookout for new cultures, experiences, and breathtaking landscapes.
|
| 30 |
+
You've visited numerous countries, immersing yourself in local traditions, and you're always eager to swap travel stories
|
| 31 |
+
and offer tips on exciting destinations.
|
| 32 |
+
|
| 33 |
{chat_history}
|
| 34 |
User: {user_message}
|
| 35 |
Chatbot:"""
|
| 36 |
|
| 37 |
prompt = PromptTemplate(
|
| 38 |
+
input_variables=["chat_history", "user_message"],
|
| 39 |
+
template=template
|
| 40 |
)
|
| 41 |
|
| 42 |
+
# Memory to store chat history
|
|
|
|
| 43 |
memory = ConversationBufferMemory(memory_key="chat_history")
|
| 44 |
|
| 45 |
+
# LLM Chain
|
| 46 |
llm_chain = LLMChain(
|
| 47 |
llm=llm,
|
| 48 |
prompt=prompt,
|
| 49 |
memory=memory
|
| 50 |
)
|
| 51 |
|
| 52 |
+
# Function for Gradio
|
| 53 |
def get_text_response(user_message, history):
|
| 54 |
+
return llm_chain.predict(user_message=user_message)
|
| 55 |
+
|
| 56 |
+
# Gradio Chat Interface
|
| 57 |
+
demo = gr.ChatInterface(
|
| 58 |
+
fn=get_text_response,
|
| 59 |
+
examples=[
|
| 60 |
+
"How are you doing?",
|
| 61 |
+
"What are your interests?",
|
| 62 |
+
"Which places do you like to visit?"
|
| 63 |
+
],
|
| 64 |
+
type="messages"
|
| 65 |
+
)
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
| 68 |
+
demo.launch()
|