import gradio as gr | |
from transformers import AutoTokenizer, AutoModelForCausalLM | |
import torch | |
# Set model ID | |
# comment/uncomment the model you want to use | |
# GPT-2 (very small, general-purpose, mainly for testing or learning purposes) | |
# model_id = "gpt2" | |
# DeepSeek Coder 1.3B (base version, no instruction fine-tuning — better for raw code generation tasks) | |
# model_id = "deepseek-ai/deepseek-coder-1.3b" | |
# DeepSeek Coder 1.3B Base (same as above — explicit base naming, safe to use) | |
# model_id = "deepseek-ai/deepseek-coder-1.3b-base" | |
# CodeLlama 7B Instruct (powerful code generation model from Meta, instruction-tuned) | |
# model_id = "codellama/CodeLlama-7b-Instruct-hf" | |
# Meta-Llama 3.1 8B Instruct (very powerful general-purpose model, instruction-following, also decent for code & NLP) | |
# model_id = "meta-llama/Llama-3.1-8B-Instruct" | |
# DeepSeek-R1 + Qwen3 8B (highly capable multi-purpose model — great for reasoning, coding, general Q&A) | |
# model_id = "deepseek-ai/DeepSeek-R1-0528-Qwen3-8B" | |
# Qwen2.5-VL-7B Instruct (multimodal: can handle text + images, instruction-tuned — mostly for vision-language tasks) | |
# model_id = "Qwen/Qwen2.5-VL-7B-Instruct" | |
# DeepSeek Coder 1.3B Instruct (great for both natural language and coding tasks) | |
model_id = "deepseek-ai/deepseek-coder-1.3b-instruct" | |
# Load tokenizer and model | |
tokenizer = AutoTokenizer.from_pretrained(model_id) | |
model = AutoModelForCausalLM.from_pretrained( | |
model_id, | |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32 | |
) | |
# Move model to GPU if available | |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
model.to(device) | |
def generate_code(prompt): | |
if not prompt.strip(): | |
return "⚠ Please enter a valid prompt." | |
inputs = tokenizer(prompt, return_tensors="pt").to(device) | |
with torch.no_grad(): | |
outputs = model.generate( | |
**inputs, | |
max_new_tokens=200, | |
temperature=0.7 | |
) | |
output_text = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
# Strip the prompt if it appears at the start | |
if output_text.startswith(prompt): | |
output_text = output_text[len(prompt):].lstrip() | |
return output_text | |
demo = gr.Interface( | |
fn=generate_code, | |
inputs=gr.Textbox(lines=5, label="Ask me a question ? or tell me to generate-code ^_^ :"), | |
outputs=gr.Textbox(label="Generated Output is:"), | |
title="Code-NLP Fusion" | |
) | |
demo.launch() | |