vuminhtue commited on
Commit
d22b8a9
·
verified ·
1 Parent(s): 50eae66

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+
4
+ # Initialize the Hugging Face Inference Client
5
+ # No API key needed for public models, but you can add one for better rate limits
6
+ client = InferenceClient()
7
+
8
+ def chatbot(question, model, temperature):
9
+ """Send the question to HuggingFace model using the Inference API.
10
+
11
+ This function sends your question to a HuggingFace model and returns the response.
12
+ """
13
+ if not question:
14
+ return "Please enter a question."
15
+
16
+ try:
17
+ # Call HuggingFace Inference API with the selected model
18
+ response = client.chat_completion(
19
+ model=model,
20
+ messages=[{"role": "user", "content": question}],
21
+ temperature=float(temperature),
22
+ max_tokens=500
23
+ )
24
+
25
+ # Extract the response content
26
+ content = response.choices[0].message.content
27
+
28
+ # Include model and temperature information in the returned text
29
+ return f"Model: {model}\nTemperature: {temperature}\n\n{content}"
30
+
31
+ except Exception as e:
32
+ return f"Error: {str(e)}\n\nPlease try again or select a different model."
33
+
34
+
35
+ def main():
36
+ # Build a simple Gradio app with a left-side control panel (model + temperature)
37
+ with gr.Blocks(title="HuggingFace Chatbot") as demo:
38
+ gr.Markdown("# 🤗 HuggingFace Chatbot")
39
+ gr.Markdown("Ask questions and get responses from various HuggingFace AI models!")
40
+
41
+ with gr.Row():
42
+ # Left column: grouped controls (model dropdown + temperature slider)
43
+ with gr.Column(scale=1):
44
+ gr.Markdown("**Model & Settings**")
45
+ model_dropdown = gr.Dropdown(
46
+ label="Model",
47
+ choices=[
48
+ "meta-llama/Llama-3.2-3B-Instruct",
49
+ "microsoft/Phi-3-mini-4k-instruct",
50
+ "mistralai/Mistral-7B-Instruct-v0.3",
51
+ "HuggingFaceH4/zephyr-7b-beta"
52
+ ],
53
+ value="meta-llama/Llama-3.2-3B-Instruct",
54
+ )
55
+ temp_slider = gr.Slider(
56
+ label="Temperature",
57
+ minimum=0.0,
58
+ maximum=1.0,
59
+ step=0.01,
60
+ value=0.7,
61
+ info="Higher values make output more random"
62
+ )
63
+
64
+ # Right column: input and output
65
+ with gr.Column(scale=3):
66
+ question = gr.Textbox(
67
+ label="Question",
68
+ lines=3,
69
+ placeholder="Type your message here..."
70
+ )
71
+ submit = gr.Button("Submit", variant="primary")
72
+ output = gr.Textbox(label="Response", lines=15)
73
+
74
+ # Wire up the button to call the chatbot function with model and temperature
75
+ submit.click(
76
+ fn=chatbot,
77
+ inputs=[question, model_dropdown, temp_slider],
78
+ outputs=output
79
+ )
80
+
81
+ # Also allow pressing Enter to submit
82
+ question.submit(
83
+ fn=chatbot,
84
+ inputs=[question, model_dropdown, temp_slider],
85
+ outputs=output
86
+ )
87
+
88
+ demo.launch()
89
+
90
+
91
+ if __name__ == "__main__":
92
+ main()
93
+