PierreJousselin commited on
Commit
8c30f17
·
verified ·
1 Parent(s): 2706c41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -27
app.py CHANGED
@@ -1,36 +1,26 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
- from transformers import AutoTokenizer
4
 
5
- # Set the model name and initialize the InferenceClient and tokenizer
6
- model_name = "PierreJousselin/lora_model" # Replace with your model's name
7
- client = InferenceClient(model_name)
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
9
 
10
- # Define a function to interact with the model
11
- def chat_with_model(input_text):
12
- # Tokenize the input text
13
- inputs = tokenizer(input_text, return_tensors="pt", truncation=True, padding=True, max_length=512)
14
- inputs["pad_token_id"] = tokenizer.pad_token_id # Set the pad token ID
15
- # Send the request to the Hugging Face Inference API
16
- result = client.text_generation(
17
- prompt=inputs["input_ids"].tolist(), # Send tokenized input
18
- )
19
 
20
- # Decode the generated text back to a readable format
21
- response = tokenizer.decode(result[0]["generated_text"], skip_special_tokens=True)
 
 
 
22
 
23
  return response
24
 
25
- # Set up the Gradio interface
26
- interface = gr.Interface(
27
- fn=chat_with_model,
28
- inputs=[gr.Textbox(lines=5, placeholder="Enter your text here...", label="Input Text")],
29
- outputs=gr.Textbox(lines=5, label="Response"),
30
- title="Hugging Face Chatbot",
31
- description="A simple chatbot powered by Hugging Face and InferenceClient."
32
- )
33
 
34
- # Launch the Gradio app
35
- if __name__ == "__main__":
36
- interface.launch()
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
 
3
 
4
+ # Load the model and tokenizer from Hugging Face
5
+ model_name = "your_huggingface_model_name" # Replace with your model's name or path
6
+ model = AutoModelForCausalLM.from_pretrained(model_name)
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
 
9
+ # Define the chat function
10
+ def chat_with_model(user_input):
11
+ # Encode the input
12
+ inputs = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors="pt")
 
 
 
 
 
13
 
14
+ # Generate a response from the model
15
+ outputs = model.generate(inputs, max_length=1000, pad_token_id=tokenizer.eos_token_id)
16
+
17
+ # Decode the model's output
18
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
19
 
20
  return response
21
 
22
+ # Set up Gradio interface
23
+ iface = gr.Interface(fn=chat_with_model, inputs="text", outputs="text", live=True)
 
 
 
 
 
 
24
 
25
+ # Launch the interface
26
+ iface.launch()