KR-16/Code-Assistant-CodeLlama2
0
1import requests2import json3import gradio as gr4import os5from huggingface_hub import InferenceClient6from dotenv import load_dotenv7 8#load the environment variables9load_dotenv()10 11# Check for API token12if not os.getenv("HUGGINGFACE_API_TOKEN"):13 raise ValueError("Please set HUGGINGFACE_API_TOKEN in your .env file")14 15client = InferenceClient(16 token=os.getenv("HUGGINGFACE_API_TOKEN")17)18 19MODELS = {20 "gpt2": "GPT-2 (124M) - General purpose model",21 "facebook/opt-125m": "OPT (125M) - Good for text generation",22 "EleutherAI/pythia-160m": "Pythia (160M) - Good for code",23 "bigscience/bloom-560m": "BLOOM (560M) - Multilingual model",24 "microsoft/phi-1": "Phi-1 (1.3B) - Good for coding tasks"25}26 27 28# headers = {29# "Content-Type": "application/json",30# "Accept": "application/json"31# }32 33# history = []34 35def generate_response(prompt, model_choice):36 try:37 response = client.text_generation(38 prompt,39 model=model_choice,40 max_new_tokens=400,41 temperature=0.7,42 top_p=0.95,43 repetition_penalty=1.15,44 do_sample=True45 )46 return response47 except Exception as e:48 return f"Error: {str(e)}\n Try with another model"49 50interface = gr.Interface(51 fn = generate_response,52 inputs = [53 gr.Textbox(54 lines=20,55 label="Let me know your questions!",56 placeholder="You can tell me the question here..."57 ),58 gr.Dropdown(59 choices=list(MODELS.keys()),60 value="microsoft/phi-1",61 label="Select Model",62 info="Choose the model to use"63 )64 ],65 outputs = gr.Textbox(66 label="Response",67 lines=20,68 max_lines=40,69 show_copy_button=True,70 interactive=False,71 container=True,72 autoscroll=True,73 scale=274 ),75 title="Code Assistant",76 description="An AI assistant that helps you with coding questions using various Hugging Face models.",77 theme="soft"78)79if __name__ == "__main__":80 interface.launch(81 share=False,82 show_error=True,83 server_port=int(os.getenv("PORT", 7860)),84 show_api=False85 )