Spaces:
Runtime error
Runtime error
concurrent requests
Browse files- app.py +62 -60
- requirements.txt +2 -1
app.py
CHANGED
@@ -4,6 +4,9 @@ from openai import OpenAI
|
|
4 |
import os
|
5 |
from dotenv import load_dotenv
|
6 |
import anthropic
|
|
|
|
|
|
|
7 |
|
8 |
# Replace 'your_api_key_here' with your actual OpenAI API key
|
9 |
# For better security, use environment variables or other secure methods to store and retrieve your API key.
|
@@ -111,74 +114,73 @@ puedes instalar unas pequeñas cortinas, que sean ligeras para que dejen pasar l
|
|
111 |
por barillas estilo bambú en la barandilla, que además aportan un toque natural.\
|
112 |
"""
|
113 |
|
114 |
-
def generate_article_openai(title):
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
{
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
temperature=1.0,
|
136 |
-
system=prompt,
|
137 |
-
messages=[
|
138 |
-
{"role": "user", "content": f"Ahora que tienes tus instrucciones y referencias redacta un artículo basado en el siguiente título.\
|
139 |
-
Título:'{title}'.\
|
140 |
-
Artículo:"}
|
141 |
-
]
|
142 |
-
)
|
143 |
-
anthropic_article = message.content[0].text
|
144 |
-
return anthropic_article
|
145 |
|
146 |
-
def
|
147 |
-
|
148 |
-
|
149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
|
151 |
-
# Define your title, description, and examples
|
152 |
-
title = "Primera demo del asistente de redacción."
|
153 |
-
description = "Esta demo trabaja con modelos de lenguaje previamente entrenados por Natasquad. Seleccione o escriba un título para generar un artículo, de click en el botón submit"
|
154 |
-
examples = [
|
155 |
-
["Qué hacer diferente en este verano"],
|
156 |
-
["Como puedo perder esos kilitos extra"],
|
157 |
-
["¿Me queda bien la ropa oscura?"]
|
158 |
-
]
|
159 |
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
text-align: center;
|
167 |
-
font-size: 1.5em; /* Adjust the font size as needed */
|
168 |
-
}
|
169 |
-
"""
|
170 |
|
171 |
iface = gr.Interface(
|
172 |
fn=generate_articles,
|
173 |
inputs=gr.Textbox(lines=2, placeholder="Enter a title here..."),
|
174 |
outputs=[
|
175 |
-
gr.Textbox(label="
|
176 |
-
gr.Textbox(label="
|
177 |
],
|
178 |
-
title=
|
179 |
-
description=
|
180 |
-
examples=
|
|
|
|
|
|
|
|
|
181 |
)
|
182 |
|
183 |
-
|
184 |
-
iface.launch()
|
|
|
4 |
import os
|
5 |
from dotenv import load_dotenv
|
6 |
import anthropic
|
7 |
+
import httpx
|
8 |
+
import asyncio
|
9 |
+
import datetime
|
10 |
|
11 |
# Replace 'your_api_key_here' with your actual OpenAI API key
|
12 |
# For better security, use environment variables or other secure methods to store and retrieve your API key.
|
|
|
114 |
por barillas estilo bambú en la barandilla, que además aportan un toque natural.\
|
115 |
"""
|
116 |
|
117 |
+
async def generate_article_openai(title):
|
118 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
119 |
+
print(f"Starting OpenAI request at: {datetime.datetime.now().time()}")
|
120 |
+
async with httpx.AsyncClient(timeout=120.0) as client:
|
121 |
+
response = await client.post(
|
122 |
+
"https://api.openai.com/v1/chat/completions",
|
123 |
+
headers={
|
124 |
+
"Content-Type": "application/json",
|
125 |
+
"Authorization": f"Bearer {api_key}"
|
126 |
+
},
|
127 |
+
json={
|
128 |
+
"model": "gpt-3.5-turbo-0125",
|
129 |
+
"messages": [
|
130 |
+
{"role": "system", "content": prompt},
|
131 |
+
{"role": "user", "content": f"Write an article based on the title: '{title}'."}
|
132 |
+
]
|
133 |
+
}
|
134 |
+
)
|
135 |
+
response_data = response.json()
|
136 |
+
print(f"OpenAI response received at: {datetime.datetime.now().time()}")
|
137 |
+
return response_data['choices'][0]['message']['content']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
|
139 |
+
async def generate_article_anthropic(title):
|
140 |
+
api_key = os.getenv("ANTHROPIC_API_KEY")
|
141 |
+
print(f"Starting Anthropic request at: {datetime.datetime.now().time()}")
|
142 |
+
async with httpx.AsyncClient(timeout=120.0) as client:
|
143 |
+
response = await client.post(
|
144 |
+
"https://api.anthropic.com/v1/messages",
|
145 |
+
headers={
|
146 |
+
"x-api-key": api_key,
|
147 |
+
"anthropic-version": "2023-06-01",
|
148 |
+
"Content-Type": "application/json"
|
149 |
+
},
|
150 |
+
json={
|
151 |
+
"model": "claude-3-sonnet-20240229",
|
152 |
+
"max_tokens": 2000,
|
153 |
+
"messages": [
|
154 |
+
{"role": "user", "content": f"Write an article based on the title: '{title}'."}
|
155 |
+
]
|
156 |
+
}
|
157 |
+
)
|
158 |
+
response_data = response.json()
|
159 |
+
print(f"Anthropic response received at: {datetime.datetime.now().time()}")
|
160 |
+
return response_data['content'][0]['text']
|
161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
|
163 |
+
async def generate_articles(title):
|
164 |
+
openai_article, anthropic_article = await asyncio.gather(
|
165 |
+
generate_article_openai(title),
|
166 |
+
generate_article_anthropic(title)
|
167 |
+
)
|
168 |
+
return openai_article, anthropic_article
|
|
|
|
|
|
|
|
|
169 |
|
170 |
iface = gr.Interface(
|
171 |
fn=generate_articles,
|
172 |
inputs=gr.Textbox(lines=2, placeholder="Enter a title here..."),
|
173 |
outputs=[
|
174 |
+
gr.Textbox(label="LLM 1 Output"),
|
175 |
+
gr.Textbox(label="LLM 2 Output")
|
176 |
],
|
177 |
+
title="Primera demo del asistente de redacción.",
|
178 |
+
description="Esta demo trabaja con modelos de lenguaje previamente entrenados por Natasquad. Seleccione o escriba un título para generar un artículo, de click en el botón submit",
|
179 |
+
examples=[
|
180 |
+
["Qué hacer diferente en este verano"],
|
181 |
+
["Como puedo perder esos kilitos extra"],
|
182 |
+
["¿Me queda bien la ropa oscura?"]
|
183 |
+
]
|
184 |
)
|
185 |
|
186 |
+
iface.launch()
|
|
requirements.txt
CHANGED
@@ -1,3 +1,4 @@
|
|
1 |
openai
|
2 |
python-dotenv
|
3 |
-
anthropic
|
|
|
|
1 |
openai
|
2 |
python-dotenv
|
3 |
+
anthropic
|
4 |
+
httpx
|