vuminhtue commited on
Commit
0fd3b67
·
verified ·
1 Parent(s): 3918194

Upload app2.py

Browse files
Files changed (1) hide show
  1. app2.py +74 -0
app2.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from huggingface_hub import InferenceClient
4
+
5
+ # Get token from environment variable for security
6
+ # In HuggingFace Spaces, set this in the Settings tab
7
+ HF_TOKEN = os.environ.get('HUGGINGFACE_TOKEN')
8
+
9
+ # Initialize the HuggingFace Inference Client
10
+ client = InferenceClient(token=HF_TOKEN)
11
+
12
+ def chatbot_hf(question, temperature=0.7, model='google/gemma-2-2b-it'):
13
+ # Send the question to HuggingFace model
14
+ response = client.chat_completion(
15
+ model=model,
16
+ messages=[{"role": "user", "content": question}],
17
+ temperature=temperature,
18
+ max_tokens=500 # Maximum length of response
19
+ )
20
+ # Extract and return the response text
21
+ return response.choices[0].message.content
22
+
23
+ def main():
24
+ # Define available models
25
+ AVAILABLE_MODELS = [
26
+ "google/gemma-2-2b-it",
27
+ "meta-llama/Llama-2-7b-chat-hf",
28
+ "mistralai/Mixtral-8x7B-Instruct-v0.1",
29
+ "HuggingFaceH4/zephyr-7b-beta"
30
+ ]
31
+
32
+ # Create the Gradio interface with a more polished layout
33
+ demo = gr.Interface(
34
+ fn=chatbot_hf,
35
+ inputs=[
36
+ gr.Textbox(
37
+ label="Your Question",
38
+ lines=2,
39
+ placeholder="Type your message here...",
40
+ scale=3
41
+ ),
42
+ gr.Slider(
43
+ label="Temperature",
44
+ minimum=0.0,
45
+ maximum=1.0,
46
+ step=0.01,
47
+ value=0.7,
48
+ info="Higher values make output more random, lower values more focused"
49
+ ),
50
+ gr.Dropdown(
51
+ label="Select Model",
52
+ choices=AVAILABLE_MODELS,
53
+ value=AVAILABLE_MODELS[0],
54
+ info="Choose the AI model to chat with"
55
+ ),
56
+ ],
57
+ outputs=gr.Textbox(label="AI Response", lines=20),
58
+ title="🤖 HuggingFace Chat Interface",
59
+ description="""
60
+ Chat with various large language models hosted on HuggingFace.
61
+ Adjust the temperature to control response creativity.
62
+ """,
63
+ article="""
64
+ ### Tips
65
+ - For factual responses, use lower temperature (0.1-0.3)
66
+ - For creative writing, use higher temperature (0.7-0.9)
67
+ - Different models may have different strengths
68
+ """
69
+ )
70
+
71
+ demo.launch()
72
+
73
+ if __name__ == "__main__":
74
+ main()