Cristobal299 commited on
Commit
f3a3f2c
·
verified ·
1 Parent(s): c1c0904

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +132 -29
app.py CHANGED
@@ -4,11 +4,13 @@ import warnings
4
 
5
  import gradio as gr
6
 
7
- # Suppress warnings that can appear in the Space environment
 
 
8
  warnings.filterwarnings("ignore", category=UserWarning)
9
 
10
  # ----------------------------------------------------------------------
11
- # Groq client setup
12
  # ----------------------------------------------------------------------
13
  groq_api_key = os.getenv("GROQ_API_KEY")
14
  groq_client = None
@@ -18,17 +20,42 @@ if groq_api_key:
18
  from groq import Groq
19
  groq_client = Groq(api_key=groq_api_key)
20
  except Exception as e:
21
- # If the library cannot be imported or instantiated, keep client as None
22
  groq_client = None
23
- print(f"Failed to initialise Groq client: {e}")
 
24
 
25
  # ----------------------------------------------------------------------
26
- # Helper function to improve code using Groq
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  # ----------------------------------------------------------------------
28
- def improve_code(original_code: str) -> str:
 
 
29
  """
30
- Sends the original code to the Groq LLM and returns an improved version.
31
- If no API key is configured, returns an informative message.
32
  """
33
  if not original_code.strip():
34
  return "Please provide some code to improve."
@@ -37,10 +64,10 @@ def improve_code(original_code: str) -> str:
37
  return "No Groq API key configured. Set the GROQ_API_KEY environment variable."
38
 
39
  prompt = (
40
- "You are a senior software engineer. Improve the following code for readability, "
41
- "efficiency, and best practices. Return only the improved code without any explanation.\n\n"
42
- "Original code:\n"
43
- f"{original_code}"
44
  )
45
 
46
  try:
@@ -56,26 +83,102 @@ def improve_code(original_code: str) -> str:
56
  return f"Error communicating with Groq: {e}"
57
 
58
  # ----------------------------------------------------------------------
59
- # Gradio interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  # ----------------------------------------------------------------------
61
  with gr.Blocks() as demo:
62
- gr.Markdown("# Code Improver")
63
- with gr.Row():
64
- with gr.Column():
65
- code_input = gr.Textbox(
66
- label="Original Code",
67
- placeholder="Paste your code here",
68
- lines=20,
69
- max_lines=200,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  )
71
- with gr.Column():
72
- code_output = gr.Textbox(
73
- label="Improved Code",
74
- placeholder="Improved code will appear here",
75
- lines=20,
76
- max_lines=200,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  )
78
- improve_btn = gr.Button("Improve")
79
- improve_btn.click(fn=improve_code, inputs=code_input, outputs=code_output)
80
 
81
  demo.launch()
 
4
 
5
  import gradio as gr
6
 
7
+ # ----------------------------------------------------------------------
8
+ # Suppress noisy warnings in the Space environment
9
+ # ----------------------------------------------------------------------
10
  warnings.filterwarnings("ignore", category=UserWarning)
11
 
12
  # ----------------------------------------------------------------------
13
+ # Groq client initialization (reads API key from environment)
14
  # ----------------------------------------------------------------------
15
  groq_api_key = os.getenv("GROQ_API_KEY")
16
  groq_client = None
 
20
  from groq import Groq
21
  groq_client = Groq(api_key=groq_api_key)
22
  except Exception as e:
23
+ print(f"Failed to import or initialise Groq client: {e}")
24
  groq_client = None
25
+ else:
26
+ print("GROQ_API_KEY not set. The app will work in demo mode with placeholder messages.")
27
 
28
  # ----------------------------------------------------------------------
29
+ # Helper: list of supported languages (common set)
30
+ # ----------------------------------------------------------------------
31
+ SUPPORTED_LANGUAGES = [
32
+ "Python",
33
+ "JavaScript",
34
+ "Java",
35
+ "C++",
36
+ "C#",
37
+ "Go",
38
+ "Ruby",
39
+ "PHP",
40
+ "TypeScript",
41
+ "HTML",
42
+ "CSS",
43
+ "Swift",
44
+ "Kotlin",
45
+ "Rust",
46
+ "Scala",
47
+ "Perl",
48
+ "R",
49
+ "Shell",
50
+ ]
51
+
52
  # ----------------------------------------------------------------------
53
+ # Function: improve existing code
54
+ # ----------------------------------------------------------------------
55
+ def improve_code(original_code: str, language: str) -> str:
56
  """
57
+ Sends the original code to the LLM and returns an improved version.
58
+ The language selector is used to guide the model.
59
  """
60
  if not original_code.strip():
61
  return "Please provide some code to improve."
 
64
  return "No Groq API key configured. Set the GROQ_API_KEY environment variable."
65
 
66
  prompt = (
67
+ f"You are a senior software engineer. Improve the following {language} code for "
68
+ "readability, efficiency and best practices. Return only the improved code "
69
+ "without any explanation.\n\n"
70
+ f"Original code:\n{original_code}"
71
  )
72
 
73
  try:
 
83
  return f"Error communicating with Groq: {e}"
84
 
85
  # ----------------------------------------------------------------------
86
+ # Function: generate full web code from an idea
87
+ # ----------------------------------------------------------------------
88
+ def generate_web_code(idea: str, language: str) -> str:
89
+ """
90
+ Generates a complete web project (HTML/CSS/JS or a single‑file script)
91
+ based on the user's idea and the selected language.
92
+ """
93
+ if not idea.strip():
94
+ return "Please describe the web idea you want to build."
95
+
96
+ if not groq_client:
97
+ return "No Groq API key configured. Set the GROQ_API_KEY environment variable."
98
+
99
+ prompt = (
100
+ f"You are a senior full‑stack developer. Based on the following description, "
101
+ f"write a complete, runnable {language} web application. Include all necessary "
102
+ "files (HTML, CSS, JavaScript, or a single script) and comment the code. "
103
+ "Return the code only, without extra explanation.\n\n"
104
+ f"Description:\n{idea}"
105
+ )
106
+
107
+ try:
108
+ response = groq_client.chat.completions.create(
109
+ model="llama-3.3-70b-versatile",
110
+ messages=[{"role": "user", "content": prompt}],
111
+ temperature=0.3,
112
+ max_tokens=4000,
113
+ )
114
+ generated = response.choices[0].message.content
115
+ return generated.strip() if generated else "No content returned from Groq."
116
+ except Exception as e:
117
+ return f"Error communicating with Groq: {e}"
118
+
119
+ # ----------------------------------------------------------------------
120
+ # Gradio UI
121
  # ----------------------------------------------------------------------
122
  with gr.Blocks() as demo:
123
+ gr.Markdown("# Code Improver & Web Generator")
124
+
125
+ with gr.Tabs():
126
+ # --------------------------------------------------------------
127
+ # Tab 1: Improve existing code
128
+ # --------------------------------------------------------------
129
+ with gr.TabItem("Improve Code"):
130
+ with gr.Row():
131
+ with gr.Column():
132
+ lang_select_improve = gr.Dropdown(
133
+ choices=SUPPORTED_LANGUAGES,
134
+ label="Language",
135
+ value="Python",
136
+ )
137
+ code_input = gr.Textbox(
138
+ label="Original Code",
139
+ placeholder="Paste your code here",
140
+ lines=15,
141
+ )
142
+ with gr.Column():
143
+ improved_output = gr.Textbox(
144
+ label="Improved Code",
145
+ placeholder="Improved code will appear here",
146
+ lines=15,
147
+ )
148
+ improve_btn = gr.Button("Improve")
149
+ improve_btn.click(
150
+ fn=improve_code,
151
+ inputs=[code_input, lang_select_improve],
152
+ outputs=improved_output,
153
  )
154
+
155
+ # --------------------------------------------------------------
156
+ # Tab 2: Generate web project from idea
157
+ # --------------------------------------------------------------
158
+ with gr.TabItem("Generate Web"):
159
+ with gr.Row():
160
+ with gr.Column():
161
+ lang_select_generate = gr.Dropdown(
162
+ choices=SUPPORTED_LANGUAGES,
163
+ label="Target Language",
164
+ value="HTML",
165
+ )
166
+ idea_input = gr.Textbox(
167
+ label="Web Idea",
168
+ placeholder="Describe the web page or app you want",
169
+ lines=12,
170
+ )
171
+ with gr.Column():
172
+ generated_output = gr.Textbox(
173
+ label="Generated Code",
174
+ placeholder="The generated code will appear here",
175
+ lines=12,
176
+ )
177
+ generate_btn = gr.Button("Generate")
178
+ generate_btn.click(
179
+ fn=generate_web_code,
180
+ inputs=[idea_input, lang_select_generate],
181
+ outputs=generated_output,
182
  )
 
 
183
 
184
  demo.launch()