Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -42,47 +42,82 @@
|
|
42 |
# if __name__ == "__main__":
|
43 |
# demo.launch()
|
44 |
|
45 |
-
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
import gradio as gr
|
47 |
-
from
|
48 |
from langchain.schema import AIMessage, HumanMessage
|
|
|
49 |
|
50 |
-
# Set OpenAI API
|
51 |
-
os.environ["OPENAI_API_KEY"] = "sk-3_mJiR5z9Q3XN-D33cgrAIYGffmMvHfu5Je1U0CW1ZT3BlbkFJA2vfSvDqZAVUyHo2JIcU91XPiAq424OSS8ci29tWMA" # Replace with your key
|
52 |
|
53 |
-
# Initialize
|
54 |
-
llm = ChatOpenAI(temperature=1.0, model=
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
# Ensure history is a list
|
59 |
-
if history is None:
|
60 |
-
history = []
|
61 |
-
|
62 |
-
# Convert the Gradio history format to LangChain message format
|
63 |
history_langchain_format = []
|
64 |
for human, ai in history:
|
65 |
history_langchain_format.append(HumanMessage(content=human))
|
66 |
history_langchain_format.append(AIMessage(content=ai))
|
67 |
|
68 |
-
# Add
|
69 |
history_langchain_format.append(HumanMessage(content=message))
|
70 |
-
|
71 |
-
# Get the model
|
72 |
gpt_response = llm(history_langchain_format)
|
|
|
|
|
|
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
# Return the response and updated history
|
78 |
-
return gpt_response.content, history
|
79 |
-
|
80 |
-
# Create a Gradio chat interface
|
81 |
-
demo = gr.ChatInterface(
|
82 |
-
fn=get_text_response,
|
83 |
-
inputs=["text", "state"],
|
84 |
-
outputs=["text", "state"]
|
85 |
-
)
|
86 |
|
87 |
if __name__ == "__main__":
|
88 |
demo.launch()
|
@@ -98,3 +133,4 @@ if __name__ == "__main__":
|
|
98 |
|
99 |
|
100 |
|
|
|
|
42 |
# if __name__ == "__main__":
|
43 |
# demo.launch()
|
44 |
|
45 |
+
# import os
|
46 |
+
# import gradio as gr
|
47 |
+
# from langchain.chat_models import ChatOpenAI
|
48 |
+
# from langchain.schema import AIMessage, HumanMessage
|
49 |
+
|
50 |
+
# # Set OpenAI API Key
|
51 |
+
# os.environ["OPENAI_API_KEY"] = "sk-3_mJiR5z9Q3XN-D33cgrAIYGffmMvHfu5Je1U0CW1ZT3BlbkFJA2vfSvDqZAVUyHo2JIcU91XPiAq424OSS8ci29tWMA" # Replace with your key
|
52 |
+
|
53 |
+
# # Initialize the ChatOpenAI model
|
54 |
+
# llm = ChatOpenAI(temperature=1.0, model="gpt-3.5-turbo-0613")
|
55 |
+
|
56 |
+
# # Function to predict response
|
57 |
+
# def get_text_response(message, history=None):
|
58 |
+
# # Ensure history is a list
|
59 |
+
# if history is None:
|
60 |
+
# history = []
|
61 |
+
|
62 |
+
# # Convert the Gradio history format to LangChain message format
|
63 |
+
# history_langchain_format = []
|
64 |
+
# for human, ai in history:
|
65 |
+
# history_langchain_format.append(HumanMessage(content=human))
|
66 |
+
# history_langchain_format.append(AIMessage(content=ai))
|
67 |
+
|
68 |
+
# # Add the new user message to the history
|
69 |
+
# history_langchain_format.append(HumanMessage(content=message))
|
70 |
+
|
71 |
+
# # Get the model's response
|
72 |
+
# gpt_response = llm(history_langchain_format)
|
73 |
+
|
74 |
+
# # Append AI response to history
|
75 |
+
# history.append((message, gpt_response.content))
|
76 |
+
|
77 |
+
# # Return the response and updated history
|
78 |
+
# return gpt_response.content, history
|
79 |
+
|
80 |
+
# # Create a Gradio chat interface
|
81 |
+
# demo = gr.ChatInterface(
|
82 |
+
# fn=get_text_response,
|
83 |
+
# inputs=["text", "state"],
|
84 |
+
# outputs=["text", "state"]
|
85 |
+
# )
|
86 |
+
|
87 |
+
# if __name__ == "__main__":
|
88 |
+
# demo.launch()
|
89 |
+
|
90 |
+
import os # Import the os module
|
91 |
+
import time
|
92 |
import gradio as gr
|
93 |
+
from langchain_community.chat_models import ChatOpenAI # Updated import based on deprecation warning
|
94 |
from langchain.schema import AIMessage, HumanMessage
|
95 |
+
import openai
|
96 |
|
97 |
+
# Set your OpenAI API key
|
98 |
+
os.environ["OPENAI_API_KEY"] = "sk-3_mJiR5z9Q3XN-D33cgrAIYGffmMvHfu5Je1U0CW1ZT3BlbkFJA2vfSvDqZAVUyHo2JIcU91XPiAq424OSS8ci29tWMA" # Replace with your OpenAI key
|
99 |
|
100 |
+
# Initialize ChatOpenAI
|
101 |
+
llm = ChatOpenAI(temperature=1.0, model='gpt-3.5-turbo-0613')
|
102 |
|
103 |
+
def predict(message, history):
|
104 |
+
# Reformat history for LangChain
|
|
|
|
|
|
|
|
|
|
|
105 |
history_langchain_format = []
|
106 |
for human, ai in history:
|
107 |
history_langchain_format.append(HumanMessage(content=human))
|
108 |
history_langchain_format.append(AIMessage(content=ai))
|
109 |
|
110 |
+
# Add latest human message
|
111 |
history_langchain_format.append(HumanMessage(content=message))
|
112 |
+
|
113 |
+
# Get response from the model
|
114 |
gpt_response = llm(history_langchain_format)
|
115 |
+
|
116 |
+
# Return response
|
117 |
+
return gpt_response.content
|
118 |
|
119 |
+
# Using ChatInterface to create a chat-style UI
|
120 |
+
demo = gr.ChatInterface(fn=predict, type="messages")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
|
122 |
if __name__ == "__main__":
|
123 |
demo.launch()
|
|
|
133 |
|
134 |
|
135 |
|
136 |
+
|