Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Get the Hugging Face API token from environment variables
|
| 6 |
+
API_URL = "https://api-inference.huggingface.co/models/EmTpro01/codellama-Code-Generator"
|
| 7 |
+
api_token = os.getenv("HF_API_TOKEN")
|
| 8 |
+
headers = {"Authorization": f"Bearer {api_token}"}
|
| 9 |
+
|
| 10 |
+
# Function to generate code from a prompt using the Inference API
|
| 11 |
+
def generate_code(prompt):
|
| 12 |
+
payload = {
|
| 13 |
+
"inputs": prompt,
|
| 14 |
+
"parameters": {
|
| 15 |
+
"max_length": 150,
|
| 16 |
+
"temperature": 0.7,
|
| 17 |
+
"top_k": 50
|
| 18 |
+
}
|
| 19 |
+
}
|
| 20 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 21 |
+
if response.status_code == 200:
|
| 22 |
+
generated_code = response.json()[0]['generated_text']
|
| 23 |
+
return generated_code
|
| 24 |
+
else:
|
| 25 |
+
return f"Error: {response.status_code}, {response.text}"
|
| 26 |
+
|
| 27 |
+
# Create the Gradio interface
|
| 28 |
+
interface = gr.Interface(
|
| 29 |
+
fn=generate_code,
|
| 30 |
+
inputs="text",
|
| 31 |
+
outputs="text",
|
| 32 |
+
title="Code Generator using Inference API",
|
| 33 |
+
description="Enter a code prompt to generate Python code using the fine-tuned model."
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# Launch the app
|
| 37 |
+
interface.launch()
|