Vikalp026var commited on
Commit
c6bc1b6
1 Parent(s): dd3298a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
4
+
5
+ # Initialize the model and tokenizer
6
+ set_seed(1234)
7
+ model_id = "Vikalp026var/gemma-2b-it-pythoncodegen"
8
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
9
+ model = AutoModelForCausalLM.from_pretrained(model_id, device_map="cuda")
10
+
11
+ # Initialize chat history
12
+ chat = []
13
+
14
+ st.title('Chat with a Language Model')
15
+ st.write("This is a simple chatbot using a fine-tuned model on GPT-2. Type 'exit' to end the conversation.")
16
+
17
+ # Text input
18
+ user_input = st.text_input("User:", key="user_input")
19
+
20
+ # Function to handle chat
21
+ def handle_chat(user_input):
22
+ if user_input:
23
+ user_turn = {"role": "user", "content": user_input}
24
+ chat.append(user_turn)
25
+ token_inputs = tokenizer.apply_chat_template(chat, tokenize=True, return_tensors="pt", add_generation_prompt=True).to("cuda")
26
+ token_outputs = model.generate(input_ids=token_inputs, do_sample=True, max_new_tokens=500, temperature=.5)
27
+ new_tokens = token_outputs[0][token_inputs.shape[-1]:]
28
+ decoded_output = tokenizer.decode(new_tokens, skip_special_tokens=True)
29
+ model_turn = {"role": "model", "content": decoded_output}
30
+ chat.append(model_turn)
31
+ return decoded_output
32
+ return ""
33
+
34
+ # Display model response
35
+ if user_input.lower() == "exit":
36
+ st.stop()
37
+ else:
38
+ response = handle_chat(user_input)
39
+ st.text_area("Model:", value=response, height=200, key="model_response")
40
+
41
+ # Run the Streamlit app
42
+ if __name__ == '__main__':
43
+ st.run()