File size: 9,711 Bytes
fcd463d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
"""
Core conversion logic for transforming Python code to Gradio apps
"""

import re
import traceback
from typing import Dict, Any
from langchain_groq import ChatGroq
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser


def convert_code(input_code: str, groq_api_key: str, model_name: str = "llama-3.3-70b-versatile") -> Dict[str, str]:
    """
    Converts input Python/IPYNB code into a Gradio app structure.
    """
    
    # Validate API key
    if not groq_api_key:
        raise ValueError("Groq API Key is required for conversion.")
    
    if not groq_api_key.startswith('gsk_'):
        raise ValueError("Invalid Groq API Key. It should start with 'gsk_'")
    
    try:
        # Initialize Groq client
        chat = ChatGroq(
            temperature=0.1,
            model_name=model_name,
            api_key=groq_api_key,
            max_retries=2,
            timeout=30
        )
    except Exception as e:
        raise ValueError(f"Failed to initialize Groq client: {str(e)}")
    
    # System prompt for conversion - UPDATED with Gradio 6.2.0
    system_prompt = """You are an expert Python developer specializing in Gradio and Hugging Face Spaces. 
    Your task is to convert the provided Python code into a deployable Gradio web application.
    
    Return your response in this exact format:
    
    ===APP_PY===
    [Complete app.py code here - NO markdown backticks or ```python]
    ===APP_PY_END===
    
    ===REQUIREMENTS_TXT===
    [Requirements.txt content here, one dependency per line - NO markdown backticks]
    ===REQUIREMENTS_TXT_END===
    
    ===README_MD===
    [README.md content here with proper YAML frontmatter]
    ===README_MD_END===
    
    CRITICAL RULES:
    1. app.py MUST be valid Python code ONLY
    2. requirements.txt MUST be plain text
    3. README.md MUST have YAML frontmatter at the top
    4. Include all necessary imports in app.py
    5. Always include: if __name__ == "__main__": demo.launch()
    6. requirements.txt must include: gradio>=6.2.0  # CHANGED FROM 4.0.0 TO 6.2.0
    
    YAML frontmatter template:
    ---
    title: Generated Gradio App
    emoji: 🚀
    colorFrom: blue
    colorTo: purple
    sdk: gradio
    sdk_version: "6.2.0"  # CHANGED FROM 4.0.0 TO 6.2.0
    app_file: app.py
    pinned: false
    ---
    
    Do NOT include markdown code blocks (```) in any output.
    """
    
    human_template = """Convert this Python code to a complete Gradio app.
    
    Code to convert:
    ```python
    {code}
    ```"""
    
    # Create the chain
    prompt = ChatPromptTemplate.from_messages([
        ("system", system_prompt),
        ("human", human_template)
    ])
    
    chain = prompt | chat | StrOutputParser()
    
    try:
        # Invoke the chain
        response = chain.invoke({"code": input_code[:50000]})
        
        # Parse the response
        result = parse_llm_response(response)
        
        # Clean and validate outputs
        result = clean_all_outputs(result)
        result = post_process_conversion(result, input_code)
        
        return result
        
    except Exception as e:
        print(f"Conversion error: {e}")
        print(f"Traceback: {traceback.format_exc()}")
        return create_basic_template(input_code)


def parse_llm_response(response: str) -> Dict[str, str]:
    """Parse the LLM response into structured components"""
    
    result = {
        "app_py": "",
        "requirements_txt": "",
        "readme_md": ""
    }
    
    patterns = [
        (r'===APP_PY===(.*?)===APP_PY_END===', "app_py"),
        (r'===REQUIREMENTS_TXT===(.*?)===REQUIREMENTS_TXT_END===', "requirements_txt"),
        (r'===README_MD===(.*?)===README_MD_END===', "readme_md")
    ]
    
    for pattern, key in patterns:
        match = re.search(pattern, response, re.DOTALL)
        if match:
            result[key] = match.group(1).strip()
    
    return result


def clean_all_outputs(result: Dict[str, str]) -> Dict[str, str]:
    """Clean markdown backticks from all outputs"""
    
    for key in ["app_py", "requirements_txt", "readme_md"]:
        if key in result and result[key]:
            content = result[key]
            
            # Remove markdown code blocks
            content = re.sub(r'^```(python|py)?\s*', '', content, flags=re.IGNORECASE)
            content = re.sub(r'\s*```$', '', content)
            content = re.sub(r'```(python|py)?\s*(.*?)\s*```', r'\2', content, flags=re.DOTALL | re.IGNORECASE)
            
            # Remove any remaining stray backticks
            content = content.replace('```', '')
            
            # Strip whitespace
            content = content.strip()
            
            result[key] = content
    
    return result


def post_process_conversion(result: Dict[str, str], original_code: str) -> Dict[str, str]:
    """Post-process and validate the conversion result"""
    
    # Ensure app.py is valid Python
    if not result.get("app_py"):
        result["app_py"] = create_basic_app_template(original_code)
    
    # Ensure requirements.txt is clean
    result["requirements_txt"] = clean_requirements(result.get("requirements_txt", ""))
    
    # Ensure README has proper format
    result["readme_md"] = clean_readme(result.get("readme_md", ""))
    
    return result


def clean_requirements(req_text: str) -> str:
    """Clean and format requirements.txt content"""
    if not req_text:
        return "gradio>=6.2.0\nlangchain-groq>=0.1.0\nhuggingface-hub>=0.20.0"  # UPDATED
    
    # Remove any markdown
    req_text = re.sub(r'```.*?\n', '', req_text, flags=re.DOTALL)
    req_text = req_text.replace('```', '')
    
    lines = []
    for line in req_text.strip().split('\n'):
        line = line.strip()
        if line and not line.startswith('#') and not line.startswith('==='):
            line = re.sub(r'[\[\]"\']', '', line)
            if line:
                lines.append(line)
    
    # Ensure essential packages are included - UPDATED VERSION
    essential_packages = ["gradio>=6.2.0", "langchain-groq>=0.1.0", "huggingface-hub>=0.20.0"]  # UPDATED
    for package in essential_packages:
        if not any(package.split('>=')[0] in line.lower() for line in lines):
            lines.insert(0, package)
    
    # Remove duplicates and sort
    return "\n".join(sorted(set(lines)))


def clean_readme(readme_text: str) -> str:
    """Clean README.md to ensure proper format"""
    if not readme_text:
        return create_default_readme()
    
    # Remove markdown code blocks
    readme_text = re.sub(r'```.*?\n', '', readme_text, flags=re.DOTALL)
    readme_text = readme_text.replace('```', '')
    
    # Check for proper YAML frontmatter
    if not re.search(r'^---\s*$', readme_text.strip(), re.MULTILINE):
        return create_default_readme()
    
    # Update sdk_version in frontmatter if it's 4.0.0
    readme_text = re.sub(
        r'sdk_version:\s*["\']4\.0\.0["\']',
        'sdk_version: "6.2.0"',
        readme_text
    )
    
    # Update gradio version in requirements if mentioned
    readme_text = re.sub(
        r'gradio>=4\.0\.0',
        'gradio>=6.2.0',
        readme_text
    )
    
    return readme_text.strip()


def create_default_readme() -> str:
    """Create a default README with proper YAML frontmatter"""
    return """---
title: Generated Gradio App
emoji: 🚀
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: "6.2.0"  # UPDATED
app_file: app.py
pinned: false
---

# Generated Gradio Application

This application was automatically generated using the Hugging Face Space Creator.

## Features

- User-friendly interface built with Gradio 6.2.0
- Ready for deployment on Hugging Face Spaces
- Includes all necessary dependencies

## How to Use

1. Launch the application
2. Enter your inputs in the provided fields
3. Click submit to see results

## Deployment

To deploy this application on Hugging Face Spaces:

1. Create a new Space on Hugging Face
2. Select Gradio as the SDK
3. Upload the generated files
4. Your app will be live automatically

## Requirements

All dependencies are listed in requirements.txt
"""


def create_basic_app_template(input_code: str) -> str:
    """Create a basic, guaranteed-valid app.py template"""
    code_snippet = input_code[:1000]
    
    return f'''import gradio as gr

# Original code snippet
{code_snippet}

# Gradio interface
def process_input(input_text):
    try:
        # Process the input
        return f"Processed: {{input_text}}"
    except Exception as e:
        return f"Error: {{str(e)}}"

# Create the Gradio app
with gr.Blocks(title="Generated Application", theme=gr.themes.Soft()) as demo:
    gr.Markdown("# Generated Application")
    
    with gr.Row():
        with gr.Column():
            input_box = gr.Textbox(
                label="Input Text",
                placeholder="Enter text to process...",
                lines=3
            )
            submit_btn = gr.Button("Process", variant="primary")
    
    with gr.Row():
        output_box = gr.Textbox(
            label="Output",
            interactive=False,
            lines=5
        )
    
    submit_btn.click(
        fn=process_input,
        inputs=[input_box],
        outputs=[output_box]
    )

if __name__ == "__main__":
    demo.launch(share=False)
'''


def create_basic_template(input_code: str) -> Dict[str, str]:
    """Create a basic template when conversion fails"""
    return {
        "app_py": create_basic_app_template(input_code),
        "requirements_txt": "gradio>=6.2.0\nlangchain-groq>=0.1.0\nhuggingface-hub>=0.20.0",  # UPDATED
        "readme_md": create_default_readme()
    }