ShermanAI commited on
Commit
a64869d
·
1 Parent(s): 273daa5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -1
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import subprocess
2
  subprocess.check_call(["pip", "install", "-q", "openai"])
3
  subprocess.check_call(["pip", "install", "-q", "gradio", "transformers", "python-dotenv"])
@@ -34,4 +35,31 @@ examples = [
34
  inputs = [gr.inputs.Textbox(label="Talk to ChatSherman: "), "state"]
35
  outputs = ["chatbot", "state"]
36
  interface = gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title=title, description=description, examples=examples)
37
- interface.launch(debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''
2
  import subprocess
3
  subprocess.check_call(["pip", "install", "-q", "openai"])
4
  subprocess.check_call(["pip", "install", "-q", "gradio", "transformers", "python-dotenv"])
 
35
  inputs = [gr.inputs.Textbox(label="Talk to ChatSherman: "), "state"]
36
  outputs = ["chatbot", "state"]
37
  interface = gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title=title, description=description, examples=examples)
38
+ interface.launch(debug=True)
39
+ '''
40
+ import openai
41
+ import gradio as gr
42
+
43
+ openai.api_key = "OPENAI_API_KEY"
44
+
45
+ def predict(message, history):
46
+ history_openai_format = []
47
+ for human, assistant in history:
48
+ history_openai_format.append({"role": "user", "content": human })
49
+ history_openai_format.append({"role": "assistant", "content":assistant})
50
+ history_openai_format.append({"role": "user", "content": message})
51
+
52
+ response = openai.ChatCompletion.create(
53
+ model='gpt-3.5-turbo',
54
+ messages= history_openai_format,
55
+ temperature=1.0,
56
+ stream=True
57
+ )
58
+
59
+ partial_message = ""
60
+ for chunk in response:
61
+ if len(chunk['choices'][0]['delta']) != 0:
62
+ partial_message = partial_message + chunk['choices'][0]['delta']['content']
63
+ yield partial_message
64
+
65
+ gr.ChatInterface(predict).queue().launch()