rajofearth commited on
Commit
1a896b7
·
verified ·
1 Parent(s): fc9f459

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Copy of Chat_Bot_Cheet_Code.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/16cr41F99_kqEpDy1yklTbVmZq31YEsBI
8
+ """
9
+
10
+ !pip install google-generativeai gradio python-dotenv
11
+ !pip install huggingface_hub
12
+ !pip install huggingface_hub --upgrade
13
+
14
+ from IPython import get_ipython
15
+ from IPython.display import display
16
+ from google import genai
17
+ import gradio as gr
18
+ import os
19
+ from dotenv import load_dotenv
20
+ from langchain.chains import LLMChain
21
+ from langchain.prompts import PromptTemplate
22
+ from langchain.memory import ConversationBufferMemory
23
+
24
+ # Initialize Gemini client
25
+ client = genai.Client(api_key="AIzaSyAJ6YoJro4DyeyTgIf23C7UyQOreDTyB24")
26
+
27
+ 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.
28
+
29
+ {chat_history}
30
+
31
+ User: {user_message}
32
+
33
+ Chatbot:"""
34
+
35
+ def respond(message, history):
36
+ # Construct the prompt with the template
37
+ prompt = template.format(chat_history=history, user_message=message)
38
+
39
+ response = client.models.generate_content(
40
+ model="gemini-2.0-flash",
41
+ contents=prompt # Pass the prompt to Gemini
42
+ )
43
+ return response.text
44
+
45
+ # Create Gradio interface
46
+ demo = gr.ChatInterface(
47
+ respond,
48
+ title="Gemini 2.0 Flash Chatbot",
49
+ description="Chat with Gemini-2.0-Flash AI model"
50
+ )
51
+
52
+ if __name__ == "__main__":
53
+ demo.launch(share=True,debug=True) # Added share parameter for public link
54
+
55
+ """##**Publishing your code to Hugging Face**"""
56
+
57
+ from huggingface_hub import notebook_login
58
+ notebook_login()
59
+
60
+ from huggingface_hub import HfApi
61
+ api = HfApi()
62
+
63
+ HUGGING_FACE_REPO_ID = "rajofearth/chatbotgemini"
64
+
65
+ """**Adding Secret Variables in Hugging Face Account:**
66
+
67
+ - Open your Space
68
+ - Click on Settings Button
69
+ - Checkout to **Variables and secrets** section
70
+ - Create New Secrets
71
+
72
+ *Note*: Make sure to add your **OPENAI_API_KEY** in Secret key
73
+ """
74
+
75
+ # Commented out IPython magic to ensure Python compatibility.
76
+ # %mkdir /content/ChatBotWithOpenAI
77
+ !wget -P /content/ChatBotWithOpenAI/ https://s3.ap-south-1.amazonaws.com/cdn1.ccbp.in/GenAI-Workshop/ChatBotWithOpenAIAndLangChain/app.py
78
+ !wget -P /content/ChatBotWithOpenAI/ https://s3.ap-south-1.amazonaws.com/cdn1.ccbp.in/GenAI-Workshop/ChatBotWithOpenAIAndLangChain/requirements.txt
79
+
80
+ # Commented out IPython magic to ensure Python compatibility.
81
+ # %cd /content/ChatBotWithOpenAI
82
+
83
+ api.upload_file(
84
+ path_or_fileobj="./requirements.txt",
85
+ path_in_repo="requirements.txt",
86
+ repo_id=HUGGING_FACE_REPO_ID,
87
+ repo_type="space")
88
+
89
+ api.upload_file(
90
+ path_or_fileobj="./app.py",
91
+ path_in_repo="app.py",
92
+ repo_id=HUGGING_FACE_REPO_ID,
93
+ repo_type="space")
94
+