File size: 1,754 Bytes
eb58fe8 8068acd eb58fe8 8068acd eb58fe8 b5333df a91bd84 eb58fe8 8068acd eb58fe8 a91bd84 eb58fe8 8068acd eb58fe8 8068acd eb58fe8 8068acd a91bd84 df87b93 8068acd eb58fe8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
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()
|