leungchunghong commited on
Commit
6db9c52
·
verified ·
1 Parent(s): abf4917

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -6
app.py CHANGED
@@ -1,8 +1,47 @@
1
- from g4f.client import Client
 
2
 
3
- client = Client()
4
- response = client.chat.completions.create(
5
- model="gpt-3.5-turbo",
6
- messages=[{"role": "user", "content": "Hello"}],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  )
8
- print(response.choices[0].message.content)
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
 
4
+ # Load the model and tokenizer from Hugging Face
5
+ model_name = "OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5" # Example model, replace with the desired model
6
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
7
+ model = AutoModelForCausalLM.from_pretrained(model_name)
8
+
9
+ # Create a pipeline for text generation
10
+ chat_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer)
11
+
12
+ # Define the function that handles chat interaction
13
+ def chat_with_model(user_input, history=[]):
14
+ # Append the user input to the history
15
+ history.append({"role": "user", "content": user_input})
16
+
17
+ # Prepare the prompt from the history
18
+ prompt = "\n".join([f"{item['role']}: {item['content']}" for item in history])
19
+
20
+ # Generate a response
21
+ response = chat_pipeline(prompt, max_length=100, num_return_sequences=1)[0]['generated_text']
22
+
23
+ # Extract only the model's response
24
+ model_response = response[len(prompt):].strip()
25
+
26
+ # Append model response to history
27
+ history.append({"role": "assistant", "content": model_response})
28
+
29
+ return model_response, history
30
+
31
+ # Define the Gradio interface
32
+ iface = gr.Interface(
33
+ fn=chat_with_model,
34
+ inputs=[
35
+ gr.inputs.Textbox(label="User Input"),
36
+ gr.inputs.State(),
37
+ ],
38
+ outputs=[
39
+ gr.outputs.Textbox(label="Chatbot Response"),
40
+ gr.outputs.State(),
41
+ ],
42
+ title="HuggingChat Clone",
43
+ description="A simple chatbot interface using an open-source model from Hugging Face."
44
  )
45
+
46
+ # Launch the interface
47
+ iface.launch(server_name="0.0.0.0", server_port=7860)