MaziyarPanahi
commited on
cleanup
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ import requests
|
|
4 |
import json
|
5 |
import os
|
6 |
|
7 |
-
MODEL = "gpt-4-0125-preview"
|
8 |
API_URL = os.getenv("API_URL")
|
9 |
API_KEY = os.getenv("API_KEY")
|
10 |
|
@@ -23,11 +22,9 @@ headers = {
|
|
23 |
|
24 |
def is_valid_json(data):
|
25 |
try:
|
26 |
-
# Attempt to parse the JSON data
|
27 |
parsed_data = json.loads(data)
|
28 |
return True, parsed_data
|
29 |
except ValueError as e:
|
30 |
-
# If an error occurs, the JSON is not valid
|
31 |
return False, str(e)
|
32 |
|
33 |
|
@@ -46,7 +43,6 @@ with gr.Blocks() as demo:
|
|
46 |
with gr.Row():
|
47 |
|
48 |
with gr.Column(scale=2):
|
49 |
-
# Define inputs for additional parameters
|
50 |
system_prompt_input = gr.Textbox(
|
51 |
label="System Prompt",
|
52 |
placeholder="Type system prompt here...",
|
@@ -86,8 +82,6 @@ with gr.Blocks() as demo:
|
|
86 |
global_repetition_penalty = repetition_penalty
|
87 |
|
88 |
def user(user_message, history):
|
89 |
-
# print(f"User: {user_message}")
|
90 |
-
# print(f"History: {history}")
|
91 |
return "", history + [[user_message, None]]
|
92 |
|
93 |
def bot(
|
@@ -107,12 +101,7 @@ with gr.Blocks() as demo:
|
|
107 |
print(f"Top K: {top_k}")
|
108 |
print(f"Repetition Penalty: {repetition_penalty}")
|
109 |
|
110 |
-
# print(f"History in bot: {history}")
|
111 |
-
# [['Capital of France', 'The capital city of France is Paris.'], ['Thansk', 'You are welcome.'], ['What is the capital of France?', '']]
|
112 |
-
# convert this to [['Capital of France', 'The capital city of France is Paris.'], ['Thansk', 'You are welcome.'], ['What is the capital of France?', '']] to list of dict of role user and assiatant
|
113 |
history_messages = [{"content": h[0], "role": "user"} for h in history if h[0]]
|
114 |
-
# let's extract the user's question which should be the last touple first element
|
115 |
-
# user_question = history[-1][0]
|
116 |
history[-1][1] = ""
|
117 |
sys_msg = [
|
118 |
{
|
@@ -140,7 +129,7 @@ with gr.Blocks() as demo:
|
|
140 |
},
|
141 |
}
|
142 |
|
143 |
-
#
|
144 |
response = requests.post(
|
145 |
url, headers=headers, data=json.dumps(data), stream=True
|
146 |
)
|
@@ -163,17 +152,11 @@ with gr.Blocks() as demo:
|
|
163 |
)
|
164 |
|
165 |
if delta_content: # Ensure there's content to print
|
166 |
-
# print(f"Bot: {delta_content}")
|
167 |
history[-1][1] += delta_content
|
168 |
-
# print(history)
|
169 |
time.sleep(0.05)
|
170 |
yield history
|
171 |
except json.JSONDecodeError as e:
|
172 |
-
print(
|
173 |
-
f"Error decoding JSON: {e} date: {data}"
|
174 |
-
) # print(delta_content, flush=True, end="")
|
175 |
-
|
176 |
-
# print(json_data['choices'][0])
|
177 |
|
178 |
msg.submit(
|
179 |
user, [msg, chatbot], [msg, chatbot], queue=True, concurrency_limit=10
|
|
|
4 |
import json
|
5 |
import os
|
6 |
|
|
|
7 |
API_URL = os.getenv("API_URL")
|
8 |
API_KEY = os.getenv("API_KEY")
|
9 |
|
|
|
22 |
|
23 |
def is_valid_json(data):
|
24 |
try:
|
|
|
25 |
parsed_data = json.loads(data)
|
26 |
return True, parsed_data
|
27 |
except ValueError as e:
|
|
|
28 |
return False, str(e)
|
29 |
|
30 |
|
|
|
43 |
with gr.Row():
|
44 |
|
45 |
with gr.Column(scale=2):
|
|
|
46 |
system_prompt_input = gr.Textbox(
|
47 |
label="System Prompt",
|
48 |
placeholder="Type system prompt here...",
|
|
|
82 |
global_repetition_penalty = repetition_penalty
|
83 |
|
84 |
def user(user_message, history):
|
|
|
|
|
85 |
return "", history + [[user_message, None]]
|
86 |
|
87 |
def bot(
|
|
|
101 |
print(f"Top K: {top_k}")
|
102 |
print(f"Repetition Penalty: {repetition_penalty}")
|
103 |
|
|
|
|
|
|
|
104 |
history_messages = [{"content": h[0], "role": "user"} for h in history if h[0]]
|
|
|
|
|
105 |
history[-1][1] = ""
|
106 |
sys_msg = [
|
107 |
{
|
|
|
129 |
},
|
130 |
}
|
131 |
|
132 |
+
# Making the POST request and streaming the response
|
133 |
response = requests.post(
|
134 |
url, headers=headers, data=json.dumps(data), stream=True
|
135 |
)
|
|
|
152 |
)
|
153 |
|
154 |
if delta_content: # Ensure there's content to print
|
|
|
155 |
history[-1][1] += delta_content
|
|
|
156 |
time.sleep(0.05)
|
157 |
yield history
|
158 |
except json.JSONDecodeError as e:
|
159 |
+
print(f"Error decoding JSON: {e} date: {data}")
|
|
|
|
|
|
|
|
|
160 |
|
161 |
msg.submit(
|
162 |
user, [msg, chatbot], [msg, chatbot], queue=True, concurrency_limit=10
|