leadingbridge commited on
Commit
edadbff
1 Parent(s): 246d72a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -21
app.py CHANGED
@@ -3,8 +3,9 @@ import numpy as np
3
  import tensorflow as tf
4
  import gradio as gr
5
  import openai
 
6
 
7
-
8
  model_path = "leadingbridge/sentiment-analysis"
9
  tokenizer = BertTokenizerFast.from_pretrained(model_path)
10
  model = TFBertForSequenceClassification.from_pretrained(model_path, id2label={0: 'negative', 1: 'positive'} )
@@ -14,27 +15,43 @@ def sentiment_analysis(text):
14
  result = pipe(text)
15
  return result
16
 
17
- def openai_chatbot(prompt):
18
- # Set up the OpenAI API client
19
- openai.api_key = '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'
20
-
21
- # Set up the model and prompt
22
- model_engine = "text-davinci-003"
23
-
24
- # Generate a response
25
- completion = openai.Completion.create(
26
- engine=model_engine,
27
- prompt=prompt,
28
- max_tokens=1024,
29
- n=1,
30
- stop=None,
31
- temperature=0.5,
 
 
 
 
 
32
  )
33
 
34
- response = completion.choices[0].text
 
 
35
 
36
- return f'🤖 {response}'
 
 
 
 
 
 
 
37
 
 
 
38
  with gr.Blocks() as demo:
39
  gr.Markdown("Choose the Chinese NLP model you want to use.")
40
  with gr.Tab("Sentiment Analysis"):
@@ -42,9 +59,11 @@ with gr.Blocks() as demo:
42
  text_button.click(fn=sentiment_analysis,inputs=gr.Textbox(placeholder="Enter a positive or negative sentence here..."),
43
  outputs=gr.Textbox(label="Sentiment Analysis"))
44
  with gr.Tab("General Chatbot"):
45
- text_button = gr.Button("proceed")
46
- text_button.click(fn=openai_chatbot,inputs=gr.Textbox(placeholder="Enter any topic you would like to discuss in Chinese"),
47
- outputs=gr.Textbox(label="Chatbot Response"))
 
 
48
 
49
 
50
  demo.launch(inline=False)
 
3
  import tensorflow as tf
4
  import gradio as gr
5
  import openai
6
+ import os
7
 
8
+ # Sentiment Analysis Pre-Trained Model
9
  model_path = "leadingbridge/sentiment-analysis"
10
  tokenizer = BertTokenizerFast.from_pretrained(model_path)
11
  model = TFBertForSequenceClassification.from_pretrained(model_path, id2label={0: 'negative', 1: 'positive'} )
 
15
  result = pipe(text)
16
  return result
17
 
18
+
19
+ # Open AI Chatbot Model
20
+ openai.api_key = "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"
21
+
22
+ start_sequence = "\nAI:"
23
+ restart_sequence = "\nHuman: "
24
+
25
+ prompt = "The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.\n\nHuman: Hello, who are you?\nAI: I am an AI created by OpenAI. How can I help you today?\nHuman: "
26
+
27
+ def openai_create(prompt):
28
+
29
+ response = openai.Completion.create(
30
+ model="text-davinci-003",
31
+ prompt=prompt,
32
+ temperature=0.9,
33
+ max_tokens=150,
34
+ top_p=1,
35
+ frequency_penalty=0,
36
+ presence_penalty=0.6,
37
+ stop=[" Human:", " AI:"]
38
  )
39
 
40
+ return response.choices[0].text
41
+
42
+
43
 
44
+ def chatgpt_clone(input, history):
45
+ history = history or []
46
+ s = list(sum(history, ()))
47
+ s.append(input)
48
+ inp = ' '.join(s)
49
+ output = openai_create(inp)
50
+ history.append((input, output))
51
+ return history, history
52
 
53
+
54
+ # Gradio Output Model
55
  with gr.Blocks() as demo:
56
  gr.Markdown("Choose the Chinese NLP model you want to use.")
57
  with gr.Tab("Sentiment Analysis"):
 
59
  text_button.click(fn=sentiment_analysis,inputs=gr.Textbox(placeholder="Enter a positive or negative sentence here..."),
60
  outputs=gr.Textbox(label="Sentiment Analysis"))
61
  with gr.Tab("General Chatbot"):
62
+ chatbot = gr.Chatbot()
63
+ message = gr.Textbox(placeholder=prompt)
64
+ state = gr.State()
65
+ submit = gr.Button("SEND")
66
+ submit.click(chatgpt_clone, inputs=[message, state], outputs=[chatbot, state])
67
 
68
 
69
  demo.launch(inline=False)