Spaces:
Sleeping
Sleeping
File size: 2,609 Bytes
1859ea0 0170f28 8e0846e 0170f28 8e0846e 0337ce6 8e0846e 0170f28 81bb13b 8e0846e 0337ce6 8e0846e 0337ce6 8e0846e 1859ea0 39ae643 81bb13b 0337ce6 1859ea0 0337ce6 81bb13b 0337ce6 4b1477c 81bb13b 4b1477c 4fb49a4 4b1477c |
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 |
import gradio as gr
import requests
import json
import os
# Retrieve the API key from environment variables
API_KEY = os.getenv('API_KEY')
if not API_KEY:
raise ValueError("API_KEY environment variable not set")
API_URL = "https://api-inference.huggingface.co/models/Salesforce/codegen-350M-mono"
headers = {"Authorization": f"Bearer {API_KEY}"}
def generate_response(prompt, max_length=500, temperature=0.5):
data = {
"inputs": prompt,
"parameters": {
"max_length": max_length,
"temperature": temperature,
}
}
response = requests.post(API_URL, headers=headers, json=data)
if response.status_code == 200:
result = response.json()
return result[0]['generated_text'] if result else "No response received"
else:
return f"Error: {response.status_code}\n{response.text}"
def main(prompt, max_length=500, temperature=0.5):
response = generate_response(prompt, max_length, temperature)
return response
# Adding real-time feedback
def real_time_feedback(prompt):
# Validate prompt and provide feedback
if len(prompt) < 10:
return "Please enter a longer prompt..."
else:
return "Ready to generate code!"
# Adding rich text formatting
def format_code(code):
# Add syntax highlighting to the generated code using HTML tags
formatted_code = "<pre><code>" + code + "</code></pre>"
return formatted_code
# Adding interactive elements
def interactive_elements(prompt, max_length=500, temperature=0.5):
# Provide interactive feedback and formatting
feedback = real_time_feedback(prompt)
code = main(prompt, max_length, temperature)
formatted_code = format_code(code)
return feedback, formatted_code
# Enhanced Gradio Interface with interactive elements
interactive_iface = gr.Interface(
fn=interactive_elements,
inputs=[
gr.Textbox(label="Enter your prompt", placeholder="Enter your prompt here...", lines=5),
gr.Slider(label="Max Length", minimum=50, maximum=2000, value=500, step=10),
gr.Slider(label="Temperature", minimum=0.1, maximum=1.0, value=0.5, step=0.1)
],
outputs=[
gr.Textbox(label="Real-Time Feedback", placeholder="Real-time feedback will appear here...", lines=1),
gr.HTML(label="Generated Code")
],
title="Enhanced Code Assistant",
description="Enter your prompt and get responses from the code generation model. Adjust the max length and temperature to customize the output."
)
# Launch the Gradio Interface
if __name__ == "__main__":
interactive_iface.launch()
|