Update app.py
Browse files
app.py
CHANGED
|
@@ -8,38 +8,39 @@ client = openai.OpenAI(
|
|
| 8 |
base_url="https://api.sambanova.ai/v1",
|
| 9 |
)
|
| 10 |
|
| 11 |
-
def
|
| 12 |
"""
|
| 13 |
-
Generates a
|
| 14 |
"""
|
| 15 |
prompt = (
|
| 16 |
-
f"
|
| 17 |
-
"
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
try:
|
| 21 |
response = client.chat.completions.create(
|
| 22 |
-
model='
|
| 23 |
messages=[
|
| 24 |
-
{"role": "system", "content": "You are a creative haiku generator for
|
| 25 |
{"role": "user", "content": prompt}
|
| 26 |
],
|
| 27 |
temperature=0.7,
|
| 28 |
top_p=0.9
|
| 29 |
)
|
| 30 |
-
|
| 31 |
-
return
|
| 32 |
except Exception as e:
|
| 33 |
-
return f"Error generating haiku: {str(e)}"
|
| 34 |
|
| 35 |
# Gradio Interface
|
| 36 |
def gradio_interface():
|
| 37 |
interface = gr.Interface(
|
| 38 |
-
fn=
|
| 39 |
-
inputs=gr.Textbox(label="
|
| 40 |
-
outputs=gr.Textbox(label="Generated
|
| 41 |
-
title="Code Haiku Generator",
|
| 42 |
-
description="
|
| 43 |
)
|
| 44 |
return interface
|
| 45 |
|
|
|
|
| 8 |
base_url="https://api.sambanova.ai/v1",
|
| 9 |
)
|
| 10 |
|
| 11 |
+
def generate_haiku_comment(code):
|
| 12 |
"""
|
| 13 |
+
Generates a haiku comment for the given code snippet.
|
| 14 |
"""
|
| 15 |
prompt = (
|
| 16 |
+
f"Analyze the following code and write a poetic haiku as a comment summarizing its purpose or essence:\n\n"
|
| 17 |
+
f"{code}\n\n"
|
| 18 |
+
"Focus on creativity and capturing the core idea."
|
| 19 |
)
|
| 20 |
|
| 21 |
try:
|
| 22 |
response = client.chat.completions.create(
|
| 23 |
+
model='Qwen2.5-Coder-32B-Instruct',
|
| 24 |
messages=[
|
| 25 |
+
{"role": "system", "content": "You are a creative haiku generator for code comments."},
|
| 26 |
{"role": "user", "content": prompt}
|
| 27 |
],
|
| 28 |
temperature=0.7,
|
| 29 |
top_p=0.9
|
| 30 |
)
|
| 31 |
+
haiku_comment = response.choices[0].message.content.strip()
|
| 32 |
+
return haiku_comment
|
| 33 |
except Exception as e:
|
| 34 |
+
return f"Error generating haiku comment: {str(e)}"
|
| 35 |
|
| 36 |
# Gradio Interface
|
| 37 |
def gradio_interface():
|
| 38 |
interface = gr.Interface(
|
| 39 |
+
fn=generate_haiku_comment,
|
| 40 |
+
inputs=gr.Textbox(label="Code Snippet"),
|
| 41 |
+
outputs=gr.Textbox(label="Generated Haiku Comment"),
|
| 42 |
+
title="Code Haiku Comment Generator",
|
| 43 |
+
description="Paste a code snippet, and let the AI generate a poetic haiku comment summarizing it!",
|
| 44 |
)
|
| 45 |
return interface
|
| 46 |
|