Spaces:
Runtime error
Runtime error
working with anthropic
Browse files- app.py +41 -13
- requirements.txt +2 -1
app.py
CHANGED
@@ -3,11 +3,13 @@ import gradio as gr
|
|
3 |
from openai import OpenAI
|
4 |
import os
|
5 |
from dotenv import load_dotenv
|
|
|
6 |
|
7 |
# Replace 'your_api_key_here' with your actual OpenAI API key
|
8 |
# For better security, use environment variables or other secure methods to store and retrieve your API key.
|
9 |
load_dotenv()
|
10 |
-
|
|
|
11 |
|
12 |
|
13 |
prompt = """
|
@@ -109,10 +111,11 @@ puedes instalar unas pequeñas cortinas, que sean ligeras para que dejen pasar l
|
|
109 |
por barillas estilo bambú en la barandilla, que además aportan un toque natural.\
|
110 |
"""
|
111 |
|
112 |
-
def
|
113 |
-
|
114 |
-
completion =
|
115 |
model="gpt-4-turbo-preview",
|
|
|
116 |
messages=[
|
117 |
{"role": "system", "content": prompt},
|
118 |
{"role": "user", "content": f"Ahora que tienes tus instrucciones y referencias redacta un artículo basado en el siguiente título.\
|
@@ -121,8 +124,29 @@ def generate_article(title):
|
|
121 |
],
|
122 |
|
123 |
)
|
124 |
-
|
125 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
# Define your title, description, and examples
|
128 |
title = "Primera demo del asistente de redacción."
|
@@ -145,12 +169,16 @@ css = """
|
|
145 |
"""
|
146 |
|
147 |
iface = gr.Interface(
|
148 |
-
fn=
|
149 |
-
inputs="
|
150 |
-
outputs=
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
|
|
|
|
155 |
)
|
|
|
|
|
156 |
iface.launch()
|
|
|
3 |
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.
|
10 |
load_dotenv()
|
11 |
+
openai_client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
12 |
+
anthropic_client = anthropic.Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
|
13 |
|
14 |
|
15 |
prompt = """
|
|
|
111 |
por barillas estilo bambú en la barandilla, que además aportan un toque natural.\
|
112 |
"""
|
113 |
|
114 |
+
def generate_article_openai(title):
|
115 |
+
openai_client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
116 |
+
completion = openai_client.chat.completions.create(
|
117 |
model="gpt-4-turbo-preview",
|
118 |
+
temperature=1.0,
|
119 |
messages=[
|
120 |
{"role": "system", "content": prompt},
|
121 |
{"role": "user", "content": f"Ahora que tienes tus instrucciones y referencias redacta un artículo basado en el siguiente título.\
|
|
|
124 |
],
|
125 |
|
126 |
)
|
127 |
+
openai_article = completion.choices[0].message.content
|
128 |
+
return openai_article
|
129 |
+
|
130 |
+
def generate_article_anthropic(title):
|
131 |
+
anthropic_client = anthropic.Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
|
132 |
+
message = anthropic_client.messages.create(
|
133 |
+
model="claude-3-sonnet-20240229",
|
134 |
+
max_tokens=2000,
|
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 generate_articles(title):
|
147 |
+
openai_article = generate_article_openai(title)
|
148 |
+
anthropic_article = generate_article_anthropic(title)
|
149 |
+
return openai_article, anthropic_article
|
150 |
|
151 |
# Define your title, description, and examples
|
152 |
title = "Primera demo del asistente de redacción."
|
|
|
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="OpenAI Output"),
|
176 |
+
gr.Textbox(label="Anthropic Output")
|
177 |
+
],
|
178 |
+
title=title,
|
179 |
+
description=description,
|
180 |
+
examples=examples
|
181 |
)
|
182 |
+
|
183 |
+
# Run the interface
|
184 |
iface.launch()
|
requirements.txt
CHANGED
@@ -1,2 +1,3 @@
|
|
1 |
openai
|
2 |
-
python-dotenv
|
|
|
|
1 |
openai
|
2 |
+
python-dotenv
|
3 |
+
anthropic
|