Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,84 +1,24 @@
|
|
1 |
-
# import os
|
2 |
-
# import gradio as gr
|
3 |
-
# from langchain.chat_models import ChatOpenAI
|
4 |
-
# from langchain import LLMChain, PromptTemplate
|
5 |
-
# from langchain.memory import ConversationBufferMemory
|
6 |
import os
|
|
|
7 |
import gradio as gr
|
8 |
-
from langchain.chat_models import ChatOpenAI
|
9 |
-
from langchain.prompts import PromptTemplate
|
10 |
-
from langchain.chains import LLMChain
|
11 |
-
from langchain.memory import ConversationBufferMemory
|
12 |
|
13 |
-
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
)
|
23 |
-
|
24 |
-
memory = ConversationBufferMemory(memory_key="chat_history")
|
25 |
-
|
26 |
-
llm_chain = LLMChain(
|
27 |
-
llm=ChatOpenAI(temperature='0.5', model_name="gpt-3.5-turbo"),
|
28 |
-
prompt=prompt,
|
29 |
-
verbose=True,
|
30 |
-
memory=memory,
|
31 |
-
)
|
32 |
-
|
33 |
-
def get_text_response(user_message,history):
|
34 |
-
response = llm_chain.predict(user_message = user_message)
|
35 |
-
return response
|
36 |
-
|
37 |
-
demo = gr.ChatInterface(get_text_response)
|
38 |
|
39 |
if __name__ == "__main__":
|
40 |
-
demo.launch()
|
41 |
-
# import os
|
42 |
-
# import gradio as gr
|
43 |
-
# from langchain.chat_models import ChatOpenAI
|
44 |
-
# from langchain.prompts import PromptTemplate
|
45 |
-
# from langchain.chains import LLMChain
|
46 |
-
# from langchain.memory import ConversationBufferMemory
|
47 |
-
|
48 |
-
# # Get API key from environment variable
|
49 |
-
# OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
50 |
-
|
51 |
-
# # Define the template for the chatbot's response
|
52 |
-
# template = """You are a helpful assistant to answer all user queries.
|
53 |
-
# {chat_history}
|
54 |
-
# User: {user_message}
|
55 |
-
# Chatbot:"""
|
56 |
-
|
57 |
-
# # Define the prompt template
|
58 |
-
# prompt = PromptTemplate(
|
59 |
-
# input_variables=["chat_history", "user_message"],
|
60 |
-
# template=template
|
61 |
-
# )
|
62 |
-
|
63 |
-
# # Initialize conversation memory
|
64 |
-
# memory = ConversationBufferMemory(memory_key="chat_history")
|
65 |
-
|
66 |
-
# # Define the LLM chain with the ChatOpenAI model and conversation memory
|
67 |
-
# llm_chain = LLMChain(
|
68 |
-
# llm=ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo"), # Use 'model' instead of 'model_name'
|
69 |
-
# prompt=prompt,
|
70 |
-
# verbose=True,
|
71 |
-
# memory=memory,
|
72 |
-
# )
|
73 |
-
|
74 |
-
# # Function to get chatbot response
|
75 |
-
# def get_text_response(user_message, history):
|
76 |
-
# response = llm_chain.predict(user_message=user_message)
|
77 |
-
# return response
|
78 |
-
|
79 |
-
# # Create a Gradio chat interface
|
80 |
-
# demo = gr.Interface(fn=get_text_response, inputs="text", outputs="text")
|
81 |
-
|
82 |
-
# if __name__ == "__main__":
|
83 |
-
# demo.launch()
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import openai
|
3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
# Set OpenAI API Key
|
6 |
+
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
7 |
+
openai.api_key = OPENAI_API_KEY
|
8 |
|
9 |
+
def get_text_response(user_message, history):
|
10 |
+
# Call OpenAI GPT model
|
11 |
+
response = openai.ChatCompletion.create(
|
12 |
+
model="gpt-3.5-turbo",
|
13 |
+
messages=[
|
14 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
15 |
+
{"role": "user", "content": user_message},
|
16 |
+
]
|
17 |
+
)
|
18 |
+
return response['choices'][0]['message']['content']
|
19 |
|
20 |
+
# Create a Gradio chat interface
|
21 |
+
demo = gr.Interface(fn=get_text_response, inputs="text", outputs="text")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
if __name__ == "__main__":
|
24 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|