File size: 2,415 Bytes
e38ea72
 
54e27fa
e38ea72
54e27fa
 
 
 
 
 
 
 
 
 
 
e38ea72
54e27fa
 
 
e38ea72
 
 
 
 
54e27fa
 
e38ea72
 
 
 
 
 
 
 
 
54e27fa
 
e38ea72
54e27fa
e38ea72
54e27fa
 
 
e38ea72
54e27fa
 
 
e38ea72
54e27fa
e38ea72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import requests
import json
import gradio as gr
import os
from huggingface_hub import InferenceClient
from dotenv import load_dotenv

#load the environment variables
load_dotenv()

# Check for API token
if not os.getenv("HUGGINGFACE_API_TOKEN"):
    raise ValueError("Please set HUGGINGFACE_API_TOKEN in your .env file")

client = InferenceClient(
    token=os.getenv("HUGGINGFACE_API_TOKEN")
)

MODELS = {
    "gpt2": "GPT-2 (124M) - General purpose model",
    "facebook/opt-125m": "OPT (125M) - Good for text generation",
    "EleutherAI/pythia-160m": "Pythia (160M) - Good for code",
    "bigscience/bloom-560m": "BLOOM (560M) - Multilingual model",
    "microsoft/phi-1": "Phi-1 (1.3B) - Good for coding tasks"
}


# headers = {
#     "Content-Type": "application/json",
#     "Accept": "application/json"
# }

# history = []

def generate_response(prompt, model_choice):
    try:
        response = client.text_generation(
            prompt,
            model=model_choice,
            max_new_tokens=400,
            temperature=0.7,
            top_p=0.95,
            repetition_penalty=1.15,
            do_sample=True
        )
        return response
    except Exception as e:
        return f"Error: {str(e)}\n Try with another model"
    
interface = gr.Interface(
    fn = generate_response,
    inputs = [
                gr.Textbox(
                    lines=20,
                    label="Let me know your questions!",
                    placeholder="You can tell me the question here..."
                ),
                gr.Dropdown(
                    choices=list(MODELS.keys()),
                    value="microsoft/phi-1",
                    label="Select Model",
                    info="Choose the model to use"
                )
            ],
            outputs = gr.Textbox(
                label="Response",
                lines=20,
                max_lines=40,
                show_copy_button=True,
                interactive=False,
                container=True,
                autoscroll=True,
                scale=2
                ),
    title="Code Assistant",
    description="An AI assistant that helps you with coding questions using various Hugging Face models.",
    theme="soft"
)
if __name__ == "__main__":
    interface.launch(
        share=False,
        show_error=True,
        server_port=int(os.getenv("PORT", 7860)),
        show_api=False
        )