Spaces:
Runtime error
Runtime error
anyscale integration
Browse files
app.py
CHANGED
@@ -114,7 +114,7 @@ puedes instalar unas pequeñas cortinas, que sean ligeras para que dejen pasar l
|
|
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:
|
@@ -125,10 +125,11 @@ async def generate_article_openai(title):
|
|
125 |
"Authorization": f"Bearer {api_key}"
|
126 |
},
|
127 |
json={
|
128 |
-
"model":
|
|
|
129 |
"messages": [
|
130 |
{"role": "system", "content": prompt},
|
131 |
-
{"role": "user", "content": f"
|
132 |
]
|
133 |
}
|
134 |
)
|
@@ -136,7 +137,7 @@ async def generate_article_openai(title):
|
|
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:
|
@@ -148,31 +149,60 @@ async def generate_article_anthropic(title):
|
|
148 |
"Content-Type": "application/json"
|
149 |
},
|
150 |
json={
|
151 |
-
"model":
|
152 |
"max_tokens": 2000,
|
|
|
|
|
153 |
"messages": [
|
154 |
-
{"role": "user", "content": f"
|
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",
|
|
|
114 |
por barillas estilo bambú en la barandilla, que además aportan un toque natural.\
|
115 |
"""
|
116 |
|
117 |
+
async def generate_article_openai(title, model):
|
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:
|
|
|
125 |
"Authorization": f"Bearer {api_key}"
|
126 |
},
|
127 |
json={
|
128 |
+
"model": model,
|
129 |
+
"temperature": 1.0,
|
130 |
"messages": [
|
131 |
{"role": "system", "content": prompt},
|
132 |
+
{"role": "user", "content": f"Siguiendo tus instrucciones y referencias redacta un artículo basado en el siguiente título. Título:'{title}'. Artículo:"}
|
133 |
]
|
134 |
}
|
135 |
)
|
|
|
137 |
print(f"OpenAI response received at: {datetime.datetime.now().time()}")
|
138 |
return response_data['choices'][0]['message']['content']
|
139 |
|
140 |
+
async def generate_article_anthropic(title, model):
|
141 |
api_key = os.getenv("ANTHROPIC_API_KEY")
|
142 |
print(f"Starting Anthropic request at: {datetime.datetime.now().time()}")
|
143 |
async with httpx.AsyncClient(timeout=120.0) as client:
|
|
|
149 |
"Content-Type": "application/json"
|
150 |
},
|
151 |
json={
|
152 |
+
"model": model,
|
153 |
"max_tokens": 2000,
|
154 |
+
"temperature": 1.0,
|
155 |
+
"system": prompt,
|
156 |
"messages": [
|
157 |
+
{"role": "user", "content": f"Siguiendo tus instrucciones y referencias redacta un artículo basado en el siguiente título. Título:'{title}'. Artículo:"}
|
158 |
]
|
159 |
}
|
160 |
)
|
161 |
response_data = response.json()
|
162 |
print(f"Anthropic response received at: {datetime.datetime.now().time()}")
|
163 |
return response_data['content'][0]['text']
|
164 |
+
|
165 |
+
async def generate_article_anyscale(title, model):
|
166 |
+
print(f"Starting Anyscale request at: {datetime.datetime.now().time()}")
|
167 |
+
api_key = os.getenv("ANYSCALE_API_KEY")
|
168 |
+
async with httpx.AsyncClient(timeout=120.0) as client:
|
169 |
+
response = await client.post(
|
170 |
+
"https://api.endpoints.anyscale.com/v1/chat/completions",
|
171 |
+
headers={
|
172 |
+
"Content-Type": "application/json",
|
173 |
+
"Authorization": f"Bearer {api_key}"
|
174 |
+
},
|
175 |
+
json={
|
176 |
+
"model": model,
|
177 |
+
"temperature": 1.0,
|
178 |
+
"messages": [
|
179 |
+
{"role": "system", "content": prompt},
|
180 |
+
{"role": "user", "content": f"Siguiendo tus instrucciones y referencias redacta un artículo basado en el siguiente título. Título:'{title}'. Artículo:"}
|
181 |
+
],
|
182 |
+
}
|
183 |
+
)
|
184 |
+
print(f"Anyscale response received at: {datetime.datetime.now().time()}")
|
185 |
+
response_data = response.json()
|
186 |
+
return response_data['choices'][0]['message']['content']
|
187 |
|
188 |
|
189 |
async def generate_articles(title):
|
190 |
+
openai_article, anthropic_article, anyscale_article_1, anyscale_article_2 = await asyncio.gather(
|
191 |
+
generate_article_openai(title, "gpt-3.5-turbo-0125"),
|
192 |
+
generate_article_anthropic(title,"claude-3-sonnet-20240229"),
|
193 |
+
generate_article_anyscale(title, "meta-llama/Meta-Llama-3-8B-Instruct"),
|
194 |
+
generate_article_anyscale(title, "mistralai/Mistral-7B-Instruct-v0.1"),
|
195 |
)
|
196 |
+
return openai_article, anthropic_article, anyscale_article_1, anyscale_article_2
|
197 |
|
198 |
iface = gr.Interface(
|
199 |
fn=generate_articles,
|
200 |
inputs=gr.Textbox(lines=2, placeholder="Enter a title here..."),
|
201 |
outputs=[
|
202 |
gr.Textbox(label="LLM 1 Output"),
|
203 |
+
gr.Textbox(label="LLM 2 Output"),
|
204 |
+
gr.Textbox(label="LLM 3 Output"),
|
205 |
+
gr.Textbox(label="LLM 4 Output"),
|
206 |
],
|
207 |
title="Primera demo del asistente de redacción.",
|
208 |
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",
|