Team Ai
Apppublic

tariquef/ProgrammingLanguageDetection

sourceHugging Faceupdated 11mo agoView on Hugging Face
0likes
app.py76 linesDownload Raw Back to root
1import gradio as gr2from transformers import pipeline3 4# Load the code language detection model5classifier = pipeline("text-classification", model="huggingface/CodeBERTa-language-id")6 7def detect_code_language(code):8    """9    Detect the programming language of the provided code snippet10    """11    if not code.strip():12        return "Please enter some code!"13    14    try:15        result = classifier(code)[0]16        language = result['label']17        confidence = result['score']18        19        # Format the output20        output = f"Detected Language: **{language.upper()}**\n\n"21        output += f"Confidence: {confidence:.2%}\n\n"22        output += "---\n\n"23        output += f"The code snippet appears to be written in **{language}** "24        output += f"with {confidence:.1%} confidence."25        26        return output27    except Exception as e:28        return f"Error: {str(e)}"29 30# Example code snippets for users to try31examples = [32    ["""def hello_world():33    print("Hello, World!")34    return True"""],35    ["""function helloWorld() {36    console.log("Hello, World!");37    return true;38}"""],39    ["""public class HelloWorld {40    public static void main(String[] args) {41        System.out.println("Hello, World!");42    }43}"""],44    ["""#include <iostream>45using namespace std;46 47int main() {48    cout << "Hello, World!" << endl;49    return 0;50}"""],51    ["""package main52import "fmt"53 54func main() {55    fmt.Println("Hello, World!")56}"""]57]58 59# Create the Gradio interface60demo = gr.Interface(61    fn=detect_code_language,62    inputs=gr.Code(63        label="Paste Your Code Here",64        language="python",65        lines=1066    ),67    outputs=gr.Markdown(label="Detection Result"),68    title="Code Language Detector",69    description="Paste any code snippet and I'll identify which programming language it's written in!",70    examples=examples,71    theme=gr.themes.Soft(),72    article="Powered by Hugging Face's CodeBERTa model. Supports Python, JavaScript, Java, C++, Go, PHP, Ruby, and more!"73)74 75if __name__ == "__main__":76    demo.launch()