Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -9,7 +9,7 @@ import requests
|
|
9 |
|
10 |
|
11 |
# define the text summarizer function
|
12 |
-
def text_prompt(request, system_role, page_urls_str,
|
13 |
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
14 |
|
15 |
page_urls = [page_url_str for page_url_str in page_urls_str.split("\n") if page_url_str]
|
@@ -44,17 +44,22 @@ def text_prompt(request, system_role, page_urls_str, contrasenya, api_base, depl
|
|
44 |
if len(tokens) > 3100:
|
45 |
break
|
46 |
page_text += sentence + ". "
|
47 |
-
|
|
|
48 |
# Delete the last space
|
49 |
page_text = page_text.strip()
|
50 |
|
51 |
num_tokens = len(tokens)
|
52 |
|
53 |
-
|
|
|
|
|
|
|
54 |
openai.api_type = "azure"
|
55 |
-
openai.api_version = "2023-
|
56 |
openai.api_base = api_base
|
57 |
-
openai.api_key =
|
|
|
58 |
# get the response from openai API
|
59 |
try:
|
60 |
response = openai.ChatCompletion.create(
|
@@ -63,7 +68,7 @@ def text_prompt(request, system_role, page_urls_str, contrasenya, api_base, depl
|
|
63 |
{"role": "system", "content": system_role},
|
64 |
{"role": "user", "content": request + "\n\n" + 'Text:\n\n""""' + page_text + '\n""""'}
|
65 |
],
|
66 |
-
max_tokens=
|
67 |
temperature=temp,
|
68 |
top_p=1.0,
|
69 |
)
|
@@ -73,16 +78,33 @@ def text_prompt(request, system_role, page_urls_str, contrasenya, api_base, depl
|
|
73 |
|
74 |
# clean the response text
|
75 |
response_text = re.sub(r'\s+', ' ', response_text)
|
76 |
-
response_text = f"#### [{page.title}]({page_url})\n\n{response_text.strip()}"
|
77 |
|
78 |
-
page_texts.append(page.text)
|
79 |
-
response_texts.append(response_text)
|
80 |
except Exception as e:
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
84 |
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
response_texts_str = "\n\n".join([response_text for response_text in response_texts])
|
87 |
total_tokens_str = str(total_tokens) + " (${:.2f} USD)".format(total_tokens / 1000 * 0.03)
|
88 |
return page_texts_str, response_texts_str, total_tokens_str
|
@@ -95,7 +117,7 @@ iface = gr.Interface(
|
|
95 |
gr.Textbox(lines=1, placeholder="Enter your system-role description here...", label="System Role:", type="text"),
|
96 |
gr.Textbox(lines=10, placeholder="Enter the Articles' URLs here...", label="Articles' URLs to parse (one per line up to 10):", type="text"),
|
97 |
gr.Textbox(lines=1, placeholder="Enter your API-key here...", label="API-Key:", type="password"),
|
98 |
-
gr.Textbox(lines=1, placeholder="Enter your Azure OpenAI
|
99 |
gr.Textbox(lines=1, placeholder="Enter your model name here...", label="Deployment ID:", type="text"),
|
100 |
gr.Slider(0.0, 1.0, value=0.0, label="Temperature:")
|
101 |
],
|
|
|
9 |
|
10 |
|
11 |
# define the text summarizer function
|
12 |
+
def text_prompt(request, system_role, page_urls_str, api_key, api_base, deployment_id, temp):
|
13 |
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
14 |
|
15 |
page_urls = [page_url_str for page_url_str in page_urls_str.split("\n") if page_url_str]
|
|
|
44 |
if len(tokens) > 3100:
|
45 |
break
|
46 |
page_text += sentence + ". "
|
47 |
+
tokens.extend(tokenizer.tokenize(request))
|
48 |
+
tokens.extend(tokenizer.tokenize(system_role))
|
49 |
# Delete the last space
|
50 |
page_text = page_text.strip()
|
51 |
|
52 |
num_tokens = len(tokens)
|
53 |
|
54 |
+
tokens_condition = num_tokens > 10
|
55 |
+
api_key_condition = len(api_key) > 6
|
56 |
+
deployment_id_condition = len(deployment_id) > 6
|
57 |
+
if tokens_condition and api_key_condition and deployment_id_condition:
|
58 |
openai.api_type = "azure"
|
59 |
+
openai.api_version = "2023-05-15"
|
60 |
openai.api_base = api_base
|
61 |
+
openai.api_key = api_key
|
62 |
+
max_tokens = 4000 - num_tokens # TODO: change 4096 to a dictionary with the max tokens for each deploymend_id
|
63 |
# get the response from openai API
|
64 |
try:
|
65 |
response = openai.ChatCompletion.create(
|
|
|
68 |
{"role": "system", "content": system_role},
|
69 |
{"role": "user", "content": request + "\n\n" + 'Text:\n\n""""' + page_text + '\n""""'}
|
70 |
],
|
71 |
+
max_tokens=max_tokens,
|
72 |
temperature=temp,
|
73 |
top_p=1.0,
|
74 |
)
|
|
|
78 |
|
79 |
# clean the response text
|
80 |
response_text = re.sub(r'\s+', ' ', response_text)
|
81 |
+
response_text = f"#### [{page.title}]({page_url})\n\n{response_text.strip()}\n"
|
82 |
|
|
|
|
|
83 |
except Exception as e:
|
84 |
+
response_text = f"#### [{page.title}]({page_url})\n\n"
|
85 |
+
response_text += f"--- An error occurred while processing the request: {e} ---\n"
|
86 |
+
page_texts.append(page.text)
|
87 |
+
response_texts.append(response_text)
|
88 |
|
89 |
+
else:
|
90 |
+
page_text_temp = "ERROR:\n\n"
|
91 |
+
if page.text:
|
92 |
+
page_text_temp += page.text
|
93 |
+
|
94 |
+
response_text_temp = "#### "
|
95 |
+
if page.title:
|
96 |
+
response_text_temp += f"[{page.title}]({page_url})"
|
97 |
+
if not tokens_condition:
|
98 |
+
response_text_temp += "\n\nERROR: Tokens problems! Maybe it can't read the URL. "
|
99 |
+
if not api_key_condition:
|
100 |
+
response_text_temp += "\n\nERROR: API Key problems! Copy and paste the API Key (be careful with copying spaces at the beginning or end of the API Key). "
|
101 |
+
if not deployment_id_condition:
|
102 |
+
response_text_temp += "\n\nERROR: Deployment_id problems! Copy and paste the deployment_id (be careful with copying spaces at the beginning or end of the deployment_id). "
|
103 |
+
|
104 |
+
page_texts.append(page_text_temp)
|
105 |
+
response_texts.append(response_text_temp)
|
106 |
+
|
107 |
+
page_texts_str = "".join([f"====== NEW URL: {URL} ======\n{page_text}\n\n" for page_text, URL in zip(page_texts, page_urls)])
|
108 |
response_texts_str = "\n\n".join([response_text for response_text in response_texts])
|
109 |
total_tokens_str = str(total_tokens) + " (${:.2f} USD)".format(total_tokens / 1000 * 0.03)
|
110 |
return page_texts_str, response_texts_str, total_tokens_str
|
|
|
117 |
gr.Textbox(lines=1, placeholder="Enter your system-role description here...", label="System Role:", type="text"),
|
118 |
gr.Textbox(lines=10, placeholder="Enter the Articles' URLs here...", label="Articles' URLs to parse (one per line up to 10):", type="text"),
|
119 |
gr.Textbox(lines=1, placeholder="Enter your API-key here...", label="API-Key:", type="password"),
|
120 |
+
gr.Textbox(lines=1, placeholder="Enter your Azure OpenAI API base here...", label="Enter Azure API base (Endpoint):", type="text"),
|
121 |
gr.Textbox(lines=1, placeholder="Enter your model name here...", label="Deployment ID:", type="text"),
|
122 |
gr.Slider(0.0, 1.0, value=0.0, label="Temperature:")
|
123 |
],
|