KR-16 commited on
Commit
54e27fa
·
verified ·
1 Parent(s): 2259224

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from huggingface_hub import InferenceClient
4
+ from dotenv import load_dotenv
5
+
6
+ #load the environment variables
7
+ load_dotenv()
8
+
9
+ # Check for API token
10
+ if not os.getenv("HUGGINGFACE_API_TOKEN"):
11
+ raise ValueError("Please set HUGGINGFACE_API_TOKEN in your .env file")
12
+
13
+ client = InferenceClient(
14
+ token=os.getenv("HUGGINGFACE_API_TOKEN"),
15
+ timeout=10
16
+ )
17
+
18
+ MODELS = {
19
+ "gpt2": "⚡ GPT-2 - Fastest general responses",
20
+ "microsoft/CodeGPT-small-py": "🐍 CodeGPT Small - Fast Python specialist",
21
+ "Salesforce/codegen-350M-mono": "🚀 CodeGen (350M) - Quick code generation",
22
+ }
23
+
24
+ def respond(message, history, model_choice):
25
+ try:
26
+ response = client.text_generation(
27
+ message,
28
+ model=model_choice,
29
+ max_new_tokens=150,
30
+ temperature=0.7,
31
+ top_p=0.95,
32
+ repetition_penalty=1.15,
33
+ do_sample=True,
34
+ return_full_text=False,
35
+ )
36
+ return response
37
+ except Exception as e:
38
+ return f"❌ Error: {str(e)}\nTry selecting a different model."
39
+
40
+ # Create chatbot interface
41
+ with gr.Blocks() as interface:
42
+ # Model selector
43
+ model_choice = gr.Dropdown(
44
+ choices=list(MODELS.keys()),
45
+ value="gpt2",
46
+ label="🔧 Select Model",
47
+ info="Choose the AI model (faster models listed first)"
48
+ )
49
+
50
+ # Chatbot component
51
+ chatbot = gr.Chatbot(
52
+ height=500, # Fixed height for scrolling
53
+ show_copy_button=True, # Enable copy button
54
+ bubble_full_width=False, # Makes it look more like a chat
55
+ )
56
+
57
+ # Message input
58
+ msg = gr.Textbox(
59
+ label="Ask your coding question",
60
+ placeholder="Enter your programming question here...",
61
+ lines=3
62
+ )
63
+
64
+ # Clear button
65
+ clear = gr.ClearButton([msg, chatbot])
66
+
67
+ # Handle message submission
68
+ msg.submit(
69
+ respond,
70
+ inputs=[msg, chatbot, model_choice],
71
+ outputs=[chatbot],
72
+ queue=False
73
+ )
74
+
75
+ # Launch the interface
76
+ interface.launch(
77
+ server_name="0.0.0.0",
78
+ server_port=7860,
79
+ share=False,
80
+ show_error=True,
81
+ )