KR-16 commited on
Commit
e38ea72
Β·
verified Β·
1 Parent(s): 9cb4a9d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -54
app.py CHANGED
@@ -1,5 +1,7 @@
1
- import os
 
2
  import gradio as gr
 
3
  from huggingface_hub import InferenceClient
4
  from dotenv import load_dotenv
5
 
@@ -11,71 +13,73 @@ 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
- )
 
 
 
 
 
1
+ import requests
2
+ import json
3
  import gradio as gr
4
+ import os
5
  from huggingface_hub import InferenceClient
6
  from dotenv import load_dotenv
7
 
 
13
  raise ValueError("Please set HUGGINGFACE_API_TOKEN in your .env file")
14
 
15
  client = InferenceClient(
16
+ token=os.getenv("HUGGINGFACE_API_TOKEN")
 
17
  )
18
 
19
  MODELS = {
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
+
35
+ def 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=True
 
45
  )
46
  return response
47
  except Exception as e:
48
+ return f"Error: {str(e)}\n Try with another model"
 
 
 
 
 
 
 
 
 
 
49
 
50
+ interface = 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=2
74
+ ),
75
+ title="Code Assistant",
76
+ description="An AI assistant that helps you with coding questions using various Hugging Face models.",
77
+ theme="soft"
78
+ )
79
+ if __name__ == "__main__":
80
+ interface.launch(
81
+ share=False,
82
+ show_error=True,
83
+ server_port=int(os.getenv("PORT", 7860)),
84
+ show_api=False
85
+ )