Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import openai
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# Set up the OpenAI client
|
6 |
+
client = openai.OpenAI(
|
7 |
+
api_key=os.environ.get("SAMBANOVA_API_KEY"),
|
8 |
+
base_url="https://api.sambanova.ai/v1",
|
9 |
+
)
|
10 |
+
|
11 |
+
def generate_code_haiku(theme):
|
12 |
+
"""
|
13 |
+
Generates a code-themed haiku based on the input theme.
|
14 |
+
"""
|
15 |
+
prompt = (
|
16 |
+
f"Write a poetic haiku about coding with the theme: '{theme}'. "
|
17 |
+
"Focus on creativity and programming metaphors."
|
18 |
+
)
|
19 |
+
|
20 |
+
try:
|
21 |
+
response = client.chat.completions.create(
|
22 |
+
model='Meta-Llama-3.1-8B-Instruct',
|
23 |
+
messages=[
|
24 |
+
{"role": "system", "content": "You are a creative haiku generator for coding themes."},
|
25 |
+
{"role": "user", "content": prompt}
|
26 |
+
],
|
27 |
+
temperature=0.7,
|
28 |
+
top_p=0.9
|
29 |
+
)
|
30 |
+
haiku = response.choices[0].message.content.strip()
|
31 |
+
return haiku
|
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=generate_code_haiku,
|
39 |
+
inputs=gr.Textbox(label="Theme (e.g., Debugging, Algorithms, AI, etc.)"),
|
40 |
+
outputs=gr.Textbox(label="Generated Code Haiku"),
|
41 |
+
title="Code Haiku Generator",
|
42 |
+
description="Generate poetic haikus about programming themes. Enter a theme, and let the AI craft a haiku for you!",
|
43 |
+
)
|
44 |
+
return interface
|
45 |
+
|
46 |
+
if __name__ == "__main__":
|
47 |
+
gradio_interface().launch()
|