HaikuCode / app.py
yasserrmd's picture
Update app.py
df87b93 verified
import os
import openai
import gradio as gr
# Set up the OpenAI client
client = openai.OpenAI(
api_key=os.environ.get("SAMBANOVA_API_KEY"),
base_url="https://api.sambanova.ai/v1",
)
def generate_haiku_comment(code):
"""
Generates a haiku comment for the given code snippet.
"""
prompt = (
f"Analyze the following code and perform two tasks:\n\n"
f"1. Write a poetic haiku as a comment summarizing its purpose or essence.\n"
f"2. Refactor and clean the code while maintaining its functionality.\n\n"
f"Code:\n{code}\n\n"
"Provide the output as haiku comment followed by the refactored code."
)
try:
response = client.chat.completions.create(
model='Qwen2.5-Coder-32B-Instruct',
messages=[
{"role": "system", "content": "You are a creative haiku generator and code refactoring assistant."},
{"role": "user", "content": prompt}
],
temperature=0.7,
top_p=0.9
)
haiku_comment = response.choices[0].message.content.strip()
return haiku_comment
except Exception as e:
return f"Error generating haiku comment: {str(e)}"
# Gradio Interface
def gradio_interface():
interface = gr.Interface(
fn=generate_haiku_comment,
inputs=gr.Textbox(label="Code Snippet",lines=10, placeholder="Paste your code snippet here..."),
outputs= gr.Textbox(label="Code will be generated herep",lines=10),
title="Code Haiku Comment Generator",
description="Paste a code snippet, and let the AI generate a poetic haiku comment summarizing it!",
)
return interface
if __name__ == "__main__":
gradio_interface().launch()