iman37 commited on
Commit
091d2f9
·
verified ·
1 Parent(s): aa7e5ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -26
app.py CHANGED
@@ -1,32 +1,39 @@
1
  import streamlit as st
2
- from transformers import AutoTokenizer, AutoModelForCausalLM
3
- import bitsandbytes as bnb
4
- import torch
5
 
6
- # Load the model and tokenizer with 4-bit quantization
7
- @st.cache_resource
8
- def load_model():
9
- tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen-7B", trust_remote_code=True)
10
- model = AutoModelForCausalLM.from_pretrained(
11
- "Qwen/Qwen-7B",
12
- load_in_4bit=True,
13
- device_map="auto",
14
- trust_remote_code=True # Allows custom code execution
15
- )
16
- return tokenizer, model
17
 
18
- tokenizer, model = load_model()
 
 
 
 
19
 
20
- # Streamlit app UI
21
- st.title("Qwen-7B Text Generation with 4-bit Quantization")
 
 
 
 
 
 
 
 
 
22
 
23
- # Text input
24
- user_input = st.text_area("Enter your text:")
25
 
26
- # Generate text on button click
27
- if st.button("Generate"):
28
- inputs = tokenizer(user_input, return_tensors="pt")
29
- outputs = model.generate(**inputs, max_length=100)
30
- generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
31
- st.write("Generated Text:")
32
- st.write(generated_text)
 
 
 
 
 
1
  import streamlit as st
2
+ from huggingface_hub import InferenceClient
3
+ import os
 
4
 
5
+ # Retrieve the Hugging Face token from environment variables
6
+ hf_token = os.getenv("HF_API_TOKEN")
 
 
 
 
 
 
 
 
 
7
 
8
+ # Initialize the Hugging Face Inference Client
9
+ client = InferenceClient(
10
+ model="Qwen/Qwen2-7B",
11
+ token=hf_token,
12
+ )
13
 
14
+ def get_chat_completion(message):
15
+ try:
16
+ response = client.chat_completion(
17
+ messages=[{"role": "user", "content": message}],
18
+ max_tokens=500,
19
+ stream=False,
20
+ )
21
+ completion = response[0]['choices'][0]['message']['content']
22
+ return completion
23
+ except Exception as e:
24
+ return f"Error: {e}"
25
 
26
+ # Streamlit app layout
27
+ st.title("Chat with Hugging Face Model")
28
 
29
+ # Input from the user
30
+ user_input = st.text_input("Enter your message:")
31
+
32
+ if st.button("Send"):
33
+ if user_input:
34
+ # Get response from the model
35
+ response = get_chat_completion(user_input)
36
+ st.write("**Response:**")
37
+ st.write(response)
38
+ else:
39
+ st.write("Please enter a message.")