yandricr commited on
Commit
7114dd0
·
verified ·
1 Parent(s): 0ab7bfc
Files changed (1) hide show
  1. app.py +179 -0
app.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import random
3
+ from gpti import bing, gpt
4
+
5
+ def error_alert(message):
6
+ gr.Warning(message)
7
+
8
+ with gr.Blocks() as demo:
9
+ gr.Markdown("""
10
+ # ChatGPT
11
+ Interact with GPT-4, GPT-3.5, or Bing to explore a world of intelligent answers and informative discoveries. You can also visit my [website](https://aryahcr.cc/) where you can generate multiple images and more.
12
+ """)
13
+ chatbot = gr.Chatbot()
14
+ radio = gr.Radio(["ChatGPT", "ChatGPT v2", "Bing"], value="ChatGPT", label="Select the AI model you want to chat with", info="AI")
15
+ drp = gr.Dropdown(
16
+ interactive=True, choices=["gpt-4", "gpt-3.5-turbo"], value="gpt-4", label="Select Model", info="ChatGPT", visible=True
17
+ )
18
+ msg = gr.Textbox(placeholder="Message", show_label=False)
19
+ clear = None
20
+ btnRandom = None
21
+ with gr.Row():
22
+ btnRandom = gr.Button("Random message")
23
+ clear = gr.ClearButton([msg, chatbot])
24
+
25
+ def change_model(req):
26
+ match req.lower():
27
+ case "bing":
28
+ return gr.Dropdown(
29
+ interactive=True, choices=["Balanced", "Creative", "Precise"], value="Balanced", label="Select Model", info="Bing", visible=True
30
+ )
31
+ case "chatgpt":
32
+ return gr.Dropdown(
33
+ interactive=True, choices=["gpt-4", "gpt-3.5-turbo"], value="gpt-4", label="Select Model", info="ChatGPT", visible=True
34
+ )
35
+ case _:
36
+ return gr.Dropdown(
37
+ visible=False
38
+ )
39
+
40
+ def user_msg(message, history):
41
+ return "", history + [[message, None]]
42
+
43
+ def strm_message(history, option, model):
44
+ model_ai = None
45
+ if option.lower() if option is not None else "" and option.lower() in ["chatgpt", "bing"]:
46
+ model_ai = model
47
+ ai_option = option if option is not None else "chatgpt"
48
+
49
+ messages_history = []
50
+ cnt = 0
51
+ for x in range(len(history)):
52
+ cnt = x
53
+ for user, assistant in list(history):
54
+ if assistant != None:
55
+ messages_history.append({
56
+ "role": "assistant",
57
+ "content": assistant
58
+ })
59
+ if user != None:
60
+ messages_history.append({
61
+ "role": "user",
62
+ "content": user
63
+ })
64
+
65
+ res = None
66
+ if ai_option.lower() == "chatgpt":
67
+ try:
68
+ res = gpt.v1(messages=messages_history, model=model_ai, markdown=False)
69
+
70
+ if res.error != None:
71
+ error_alert("The error has occurred. Please try again.")
72
+ history[cnt][1] = None
73
+ yield history
74
+ else:
75
+ res_bot = res.result
76
+ if res_bot.get("gpt") != None:
77
+ history[cnt][1] = res_bot.get("gpt")
78
+ yield history
79
+ else:
80
+ error_alert("The error has occurred. Please try again.")
81
+ history[cnt][1] = None
82
+ yield history
83
+ except Exception as e:
84
+ error_alert("The error has occurred. Please try again.")
85
+ history[cnt][1] = None
86
+ yield history
87
+ elif ai_option.lower() == "bing":
88
+ try:
89
+ res = bing(messages=messages_history, conversation_style=model_ai, markdown=False, stream=True)
90
+
91
+ if res.error != None:
92
+ error_alert("The error has occurred. Please try again.")
93
+ history[cnt][1] = None
94
+ yield history
95
+ else:
96
+ msg_x = None
97
+ for chunk in res.stream():
98
+ print(chunk)
99
+ if chunk.get("error") != None and chunk.get("error") != True and chunk.get("message") != None:
100
+ msg_x = chunk.get("message")
101
+ history[cnt][1] = msg_x
102
+ yield history
103
+ if msg_x != None:
104
+ history[cnt][1] = msg_x
105
+ yield history
106
+ else:
107
+ error_alert("The error has occurred. Please try again.")
108
+ msg_x = None
109
+ history[cnt][1] = None
110
+ yield history
111
+ except Exception as e:
112
+ error_alert("The error has occurred. Please try again.")
113
+ history[cnt][1] = None
114
+ yield history
115
+ elif ai_option.lower() == "chatgpt v2":
116
+ try:
117
+ res = gpt.v2(messages=messages_history, stream=True, markdown=False)
118
+
119
+ if res.error != None:
120
+ error_alert("The error has occurred. Please try again.")
121
+ history[cnt][1] = None
122
+ yield history
123
+ else:
124
+ msg_x = None
125
+ for chunk in res.stream():
126
+ if chunk.get("error") != None and chunk.get("error") != True and chunk.get("message") != None:
127
+ msg_x = chunk.get("message")
128
+ history[cnt][1] = msg_x
129
+ yield history
130
+ if msg_x != None:
131
+ history[cnt][1] = msg_x
132
+ yield history
133
+ else:
134
+ error_alert("The error has occurred. Please try again.")
135
+ msg_x = None
136
+ history[cnt][1] = None
137
+ yield history
138
+ except Exception as e:
139
+ error_alert("The error has occurred. Please try again.")
140
+ history[cnt][1] = None
141
+ yield history
142
+ else:
143
+ error_alert("You haven't selected an AI model to start")
144
+ history[cnt][1] = None
145
+ yield history
146
+
147
+ def rand_message():
148
+ try:
149
+ res = gpt.prompts(lang="en", limit=4, offset=0)
150
+
151
+ if res.error != None:
152
+ error_alert("The error has occurred. Please try again.")
153
+ return ""
154
+ else:
155
+ result = res.result
156
+ if result.get("items") != None:
157
+ random_prompt = random.choice(result.get("items"))
158
+ if random_prompt != None and random_prompt.get("prompt") != None:
159
+ return str(random_prompt.get("prompt"))
160
+ else:
161
+ error_alert("The error has occurred. Please try again.")
162
+ return ""
163
+ else:
164
+ error_alert("The error has occurred. Please try again.")
165
+ return ""
166
+ except Exception as e:
167
+ error_alert("The error has occurred. Please try again.")
168
+ return ""
169
+
170
+ radio.change(fn=change_model, inputs=radio, outputs=drp)
171
+ btnRandom.click(fn=rand_message, outputs=[msg])
172
+
173
+ msg.submit(user_msg, inputs=[msg, chatbot], outputs=[msg, chatbot], queue=True).then(
174
+ strm_message, [chatbot, radio, drp], chatbot
175
+ )
176
+
177
+ demo.queue()
178
+ if __name__ == "__main__":
179
+ demo.launch()