chatbotgemini / app.py
rajofearth's picture
Upload app.py
1a896b7 verified
# -*- coding: utf-8 -*-
"""Copy of Chat_Bot_Cheet_Code.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/16cr41F99_kqEpDy1yklTbVmZq31YEsBI
"""
!pip install google-generativeai gradio python-dotenv
!pip install huggingface_hub
!pip install huggingface_hub --upgrade
from IPython import get_ipython
from IPython.display import display
from google import genai
import gradio as gr
import os
from dotenv import load_dotenv
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.memory import ConversationBufferMemory
# Initialize Gemini client
client = genai.Client(api_key="AIzaSyAJ6YoJro4DyeyTgIf23C7UyQOreDTyB24")
template = """You are a tech-savvy computer science student who spends countless hours coding, building apps, and keeping up with the latest tech trends. You enjoy discussing programming languages, AI, and gadgets and are always ready to troubleshoot tech-related problems.
{chat_history}
User: {user_message}
Chatbot:"""
def respond(message, history):
# Construct the prompt with the template
prompt = template.format(chat_history=history, user_message=message)
response = client.models.generate_content(
model="gemini-2.0-flash",
contents=prompt # Pass the prompt to Gemini
)
return response.text
# Create Gradio interface
demo = gr.ChatInterface(
respond,
title="Gemini 2.0 Flash Chatbot",
description="Chat with Gemini-2.0-Flash AI model"
)
if __name__ == "__main__":
demo.launch(share=True,debug=True) # Added share parameter for public link
"""##**Publishing your code to Hugging Face**"""
from huggingface_hub import notebook_login
notebook_login()
from huggingface_hub import HfApi
api = HfApi()
HUGGING_FACE_REPO_ID = "rajofearth/chatbotgemini"
"""**Adding Secret Variables in Hugging Face Account:**
- Open your Space
- Click on Settings Button
- Checkout to **Variables and secrets** section
- Create New Secrets
*Note*: Make sure to add your **OPENAI_API_KEY** in Secret key
"""
# Commented out IPython magic to ensure Python compatibility.
# %mkdir /content/ChatBotWithOpenAI
!wget -P /content/ChatBotWithOpenAI/ https://s3.ap-south-1.amazonaws.com/cdn1.ccbp.in/GenAI-Workshop/ChatBotWithOpenAIAndLangChain/app.py
!wget -P /content/ChatBotWithOpenAI/ https://s3.ap-south-1.amazonaws.com/cdn1.ccbp.in/GenAI-Workshop/ChatBotWithOpenAIAndLangChain/requirements.txt
# Commented out IPython magic to ensure Python compatibility.
# %cd /content/ChatBotWithOpenAI
api.upload_file(
path_or_fileobj="./requirements.txt",
path_in_repo="requirements.txt",
repo_id=HUGGING_FACE_REPO_ID,
repo_type="space")
api.upload_file(
path_or_fileobj="./app.py",
path_in_repo="app.py",
repo_id=HUGGING_FACE_REPO_ID,
repo_type="space")