Spaces:
Runtime error
Runtime error
Dagfinn1962
commited on
Commit
•
86f5d1e
1
Parent(s):
e9590e5
Create test.py
Browse files
test.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import openai
|
4 |
+
import gradio as gr
|
5 |
+
from gradio import ChatInterface
|
6 |
+
import time
|
7 |
+
|
8 |
+
# Get the value of the openai_api_key from environment variable
|
9 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
10 |
+
|
11 |
+
# Import things that are needed generically from langchain
|
12 |
+
from langchain import LLMMathChain, SerpAPIWrapper
|
13 |
+
from langchain.agents import AgentType, initialize_agent, load_tools
|
14 |
+
from langchain.chat_models import ChatOpenAI
|
15 |
+
from langchain.tools import BaseTool, StructuredTool, Tool, tool
|
16 |
+
from langchain.tools import MoveFileTool, format_tool_to_openai_function
|
17 |
+
from langchain.schema import (
|
18 |
+
AIMessage,
|
19 |
+
HumanMessage,
|
20 |
+
SystemMessage
|
21 |
+
)
|
22 |
+
from langchain.utilities import WikipediaAPIWrapper
|
23 |
+
from langchain.tools import AIPluginTool
|
24 |
+
|
25 |
+
def predict(inputs, chatbot):
|
26 |
+
|
27 |
+
print(chatbot)
|
28 |
+
|
29 |
+
messages = []
|
30 |
+
for conv in chatbot:
|
31 |
+
user = conv[0]
|
32 |
+
messages.append({"role": "user", "content":user })
|
33 |
+
assistant = conv[1]
|
34 |
+
messages.append({"role": "assistant", "content":assistant})
|
35 |
+
messages.append({"role": "user", "content": inputs})
|
36 |
+
|
37 |
+
# a ChatCompletion request
|
38 |
+
response = openai.ChatCompletion.create(
|
39 |
+
model='gpt-3.5-turbo',
|
40 |
+
messages= messages, # example : [{'role': 'user', 'content': "What is life? Answer in three words."}],
|
41 |
+
temperature=1.0,
|
42 |
+
stream=True # for streaming the output to chatbot
|
43 |
+
)
|
44 |
+
|
45 |
+
partial_message = ""
|
46 |
+
for chunk in response:
|
47 |
+
if len(chunk['choices'][0]['delta']) != 0:
|
48 |
+
print(chunk['choices'][0]['delta']['content'])
|
49 |
+
partial_message = partial_message + chunk['choices'][0]['delta']['content']
|
50 |
+
yield partial_message
|
51 |
+
|
52 |
+
|
53 |
+
interface = gr.ChatInterface(predict)
|
54 |
+
with gr.Blocks() as demo:
|
55 |
+
gr.Markdown("""
|
56 |
+
# GPT 3.5 Discord Bot powered by gradio!
|
57 |
+
To use this space as a discord bot, first install the gradio_client
|
58 |
+
|
59 |
+
```bash
|
60 |
+
pip install gradio_client
|
61 |
+
```
|
62 |
+
|
63 |
+
Then run the following command
|
64 |
+
|
65 |
+
```python
|
66 |
+
grc.Client.duplicate("gradio-discord-bots/gpt-35-turbo", private=False, secrets={"OPENAI_API_KEY": "<your-key-here>"}).deploy_discord()
|
67 |
+
""")
|
68 |
+
with gr.Row(visible=False):
|
69 |
+
interface.render()
|
70 |
+
|
71 |
+
demo.queue(concurrency_count=100).launch()
|