ysharma HF staff commited on
Commit
94621a1
·
1 Parent(s): 2601fdc

create app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import openai
4
+ import gradio as gr
5
+ # Get the value of the openai_api_key from environment variable
6
+ openai.api_key = os.getenv("OPENAI_API_KEY")
7
+
8
+ # Import things that are needed generically from langchain
9
+ from langchain import LLMMathChain, SerpAPIWrapper
10
+ from langchain.agents import AgentType, initialize_agent, load_tools
11
+ from langchain.chat_models import ChatOpenAI
12
+ from langchain.tools import BaseTool, StructuredTool, Tool, tool
13
+ from langchain.tools import MoveFileTool, format_tool_to_openai_function
14
+ from langchain.schema import (
15
+ AIMessage,
16
+ HumanMessage,
17
+ SystemMessage
18
+ )
19
+ from langchain.utilities import WikipediaAPIWrapper
20
+ from langchain.tools import AIPluginTool
21
+
22
+
23
+ # Setting up a system message for our Chatbot
24
+ #system = SystemMessage(content = "You are a helpful AI assistant") # that translates English to Pirate English.")
25
+
26
+ # driver
27
+ def predict(user_input, chatbot):
28
+
29
+ print(f"chatbot - {chatbot}")
30
+ print(f"user_input - {user_input}")
31
+
32
+ chat = ChatOpenAI(
33
+ #openai_api_key=openai_api_key,
34
+ temperature=1.0, #temperature, #1.0
35
+ streaming=True,
36
+ model='gpt-3.5-turbo-0613')
37
+ #messages = [system]
38
+ messages=[]
39
+ #function_call_decision = True if any(plugins) else False
40
+
41
+ if len(chatbot) != 0:
42
+ for conv in chatbot:
43
+ human = HumanMessage(content=conv[0])
44
+ ai = AIMessage(content=conv[1])
45
+ messages.append(human)
46
+ messages.append(ai)
47
+ messages.append(HumanMessage(content=user_input))
48
+ print(f"messages list is - {messages}")
49
+
50
+ else: # for first user message
51
+ messages.append(HumanMessage(content=user_input))
52
+ print(f"messages list is - {messages}")
53
+
54
+ # getting gpt3.5's response
55
+ gpt_response = chat(messages)
56
+ print(f"gpt_response - {gpt_response}")
57
+ bot_message = gpt_response.content
58
+ print(f"bot_message - {bot_message}")
59
+
60
+ chatbot.append((user_input, bot_message))
61
+
62
+ #return "", chatbot, None #"", chatbot
63
+ return bot_message
64
+
65
+ #chatbot = gr.Chatbot()
66
+ gr.ChatInterface(predict, delete_last_btn="del").launch(share=False, debug=True) #examples=["How are you?", "What's up?"],