File size: 1,650 Bytes
1e498fc
 
 
4080d21
1e498fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4080d21
 
 
1e498fc
 
 
 
 
575a5c5
 
1e498fc
 
 
 
4080d21
 
1e498fc
 
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
import gradio as gr
import requests
import os

API_URL = "https://api-inference.huggingface.co/models/openai-gpt"
API_TOKEN = os.environ.get("API_TOKEN")

headers = {"Authorization": f"Bearer {API_TOKEN}"}

# Function to translate code using the Hugging Face model API
# Function to translate code using the Hugging Face model API
# Function to translate code using the Hugging Face model API
def translate_code(input_text, source_lang, target_lang):
    payload = {
        "inputs": f"convert the below {source_lang} code to {target_lang} code: {input_text}"
    }
    
    response = requests.post(API_URL, headers=headers, json=payload)
    response_data = response.json()  # Store the entire response for inspection
    print("API Response:", response_data)  # Print the response for inspection
    
    # Extract the translated code from the response
    translated_code = "No translation available"  # Default value
    
    if response_data:
        if isinstance(response_data, list) and len(response_data) > 0:
            translated_code = response_data[0].get("generated_text", "").strip()
    
    return translated_code



# Interface for the Gradio app
iface = gr.Interface(
    fn=translate_code,
    inputs=[
        gr.inputs.Textbox(label="Enter code to translate"),
        gr.inputs.Textbox(label="Source Language (e.g., C++,python,java...)"),
        gr.inputs.Textbox(label="Target Language (e.g., C++,python,java...)")
    ],
    outputs=gr.outputs.Textbox(label="Translated Code"),
    title="Code Translator",
    description="Translate code snippets between programming languages"
)

# Launch the Gradio app
iface.launch()