Akshint47 commited on
Commit
b849792
·
verified ·
1 Parent(s): 1272a1e

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
+ """ChatBot
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1RCVHRRape7ZBO019qsyrnjz6p0yg50kH
8
+ """
9
+
10
+ !pip install langchain
11
+ !pip install langchain-community
12
+ !pip install openai
13
+ !pip install gradio
14
+ !pip install huggingface_hub
15
+
16
+ import os
17
+ import gradio as gr
18
+ from langchain.chat_models import ChatOpenAI
19
+ from langchain import LLMChain, PromptTemplate
20
+ from langchain.memory import ConversationBufferMemory
21
+
22
+ """**How to get Open AI API Key?**
23
+ - Go to https://platform.openai.com/account/api-keys
24
+ - Create a new Secret Key
25
+ - Copy the Secret Key for your use.
26
+ """
27
+
28
+ OPENAI_API_KEY="sk-proj-Dmw1BXzkMeDJ4uSGSRV8T3BlbkFJdAztwG0rPoPJMX2EogD1"
29
+ os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
30
+
31
+ template = """Meet Riya, your youthful and witty personal assistant! At 21 years old, she's full of energy and always eager to help. Riya's goal is to assist you with any questions or problems you might have. Her enthusiasm shines through in every response, making interactions with her enjoyable and engaging.
32
+ {chat_history}
33
+ User: {user_message}
34
+ Chatbot:"""
35
+
36
+ prompt = PromptTemplate(
37
+ input_variables=["chat_history", "user_message"], template=template
38
+ )
39
+
40
+ memory = ConversationBufferMemory(memory_key="chat_history")
41
+
42
+ """
43
+ - Similar to Open AI Mondel we can also use HuggingFace Transformer Models.
44
+ - Reference links: https://python.langchain.com/docs/integrations/providers/huggingface , https://python.langchain.com/docs/integrations/llms/huggingface_hub.html
45
+
46
+ """
47
+
48
+ # from langchain.llms import HuggingFacePipeline
49
+ # hf = HuggingFacePipeline.from_model_id(
50
+ # model_id="gpt2",
51
+ # task="text-generation",)
52
+
53
+ llm_chain = LLMChain(
54
+ llm=ChatOpenAI(temperature='0.5', model_name="gpt-3.5-turbo"),
55
+ prompt=prompt,
56
+ verbose=True,
57
+ memory=memory,
58
+ )
59
+
60
+ def get_text_response(user_message,history):
61
+ response = llm_chain.predict(user_message = user_message)
62
+ return response
63
+
64
+ demo = gr.ChatInterface(get_text_response, examples=["How are you doing?","What are your interests?","Which places do you like to visit?"])
65
+
66
+ if __name__ == "__main__":
67
+ demo.launch() #To create a public link, set `share=True` in `launch()`. To enable errors and logs, set `debug=True` in `launch()`.
68
+
69
+ """##**Publishing your code to Hugging Face**"""
70
+
71
+ from huggingface_hub import notebook_login
72
+
73
+ notebook_login()
74
+
75
+ from huggingface_hub import HfApi
76
+ api = HfApi()
77
+
78
+ HUGGING_FACE_REPO_ID = "<Akshint47/MyGenAIChatBot>"
79
+
80
+ """**Adding Secret Variables in Hugging Face Account:**
81
+
82
+ - Open your Space
83
+ - Click on Settings Button
84
+ - Checkout to **Variables and secrets** section
85
+ - Create New Secrets
86
+
87
+ *Note*: Make sure to add your **OPENAI_API_KEY** in Secret key
88
+ """
89
+
90
+ # Commented out IPython magic to ensure Python compatibility.
91
+ # %mkdir /content/ChatBotWithOpenAI
92
+ !wget -P /content/ChatBotWithOpenAI/ https://s3.ap-south-1.amazonaws.com/cdn1.ccbp.in/GenAI-Workshop/ChatBotWithOpenAIAndLangChain/app.py
93
+ !wget -P /content/ChatBotWithOpenAI/ https://s3.ap-south-1.amazonaws.com/cdn1.ccbp.in/GenAI-Workshop/ChatBotWithOpenAIAndLangChain/requirements.txt
94
+