Arturo Jiménez de los Galanes Reguillos
commited on
Commit
·
fc3e2f8
1
Parent(s):
baf0ed1
Initial commit. Translate application code.
Browse files
.env
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
ANTHROPIC_API_KEY=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
|
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
import anthropic
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
load_dotenv()
|
7 |
+
|
8 |
+
os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY')
|
9 |
+
claude = anthropic.Anthropic()
|
10 |
+
CLAUDE_MODEL = "claude-3-5-sonnet-20241022"
|
11 |
+
|
12 |
+
system_message = "You are an assistant that reimplements Python code in high performance C++. \
|
13 |
+
Respond only with C++ code; use comments sparingly and do not provide any explanation other than occasional comments. \
|
14 |
+
The C++ response needs to produce an identical output in the fastest possible time."
|
15 |
+
|
16 |
+
def user_prompt_for(python):
|
17 |
+
user_prompt = f"Rewrite this Python code in C++ with the fastest possible implementation that produces identical output in the least time. \
|
18 |
+
Respond only with C++ code; do not explain your work other than a few comments. \
|
19 |
+
Pay attention to number types to ensure no int overflows. Remember to #include all necessary C++ packages such as iomanip.\
|
20 |
+
\n\n{python}"
|
21 |
+
|
22 |
+
return user_prompt
|
23 |
+
|
24 |
+
def rewrite(python):
|
25 |
+
result = claude.messages.stream(
|
26 |
+
model=CLAUDE_MODEL,
|
27 |
+
max_tokens=2000,
|
28 |
+
system=system_message,
|
29 |
+
messages=[{"role": "user", "content": user_prompt_for(python)}],
|
30 |
+
)
|
31 |
+
|
32 |
+
reply = ""
|
33 |
+
with result as stream:
|
34 |
+
for text in stream.text_stream:
|
35 |
+
reply += text
|
36 |
+
yield reply.replace('```cpp\n','').replace('```','')
|
37 |
+
|
38 |
+
demo = gr.Interface(
|
39 |
+
fn=rewrite,
|
40 |
+
inputs=gr.Code(label="Python code:", language="python", lines=10),
|
41 |
+
outputs=gr.Code(label="C++ code:", language="cpp", lines=10)
|
42 |
+
)
|
43 |
+
demo.launch()
|