krisha06 commited on
Commit
8380fa7
·
verified ·
1 Parent(s): 3fe3362

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -15
app.py CHANGED
@@ -1,29 +1,55 @@
1
  import streamlit as st
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
- import torch
 
4
 
5
- # Load the fine-tuned model
6
- model_name = "./tuned_model" # Load from Hugging Face or locally
7
- model = AutoModelForCausalLM.from_pretrained(model_name)
8
- tokenizer = AutoTokenizer.from_pretrained(model_name)
9
 
10
- # Set up Streamlit UI
11
  st.title("AI Coding Mentor")
12
- st.write("Ask me any programming-related question!")
13
 
14
- # User input (question)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  question = st.text_input("Enter your coding question:")
16
 
17
  if question:
18
- # Prepare the input for the model
19
  input_text = f"### Question:\n{question}\n### Answer:"
20
  inputs = tokenizer(input_text, return_tensors="pt")
21
 
22
- # Generate the answer using the fine-tuned model
23
- output = model.generate(**inputs, max_length=200, num_return_sequences=1)
24
-
25
- # Decode the output
26
- answer = tokenizer.decode(output[0], skip_special_tokens=True)
27
 
28
- # Display the result
29
  st.write(answer)
 
1
  import streamlit as st
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ from huggingface_hub import Repository
4
+ import os
5
 
6
+ # Define model directory and Hugging Face repo name
7
+ model_dir = "./tuned_model" # Directory where the fine-tuned model is saved
8
+ repo_name = "krisha06/Python_tutor" # Replace with your Hugging Face repo name
 
9
 
10
+ # Streamlit App Title
11
  st.title("AI Coding Mentor")
 
12
 
13
+ # Section to upload the fine-tuned model to Hugging Face Hub
14
+ st.header("Upload Your Fine-Tuned Model to Hugging Face Hub")
15
+
16
+ # Option to upload the model
17
+ upload_model_button = st.button("Upload Model to Hugging Face Hub")
18
+
19
+ if upload_model_button:
20
+ if os.path.exists(model_dir):
21
+ # Initialize the Hugging Face Repository and push model to the Hub
22
+ repo = Repository(local_dir=model_dir, clone_from=repo_name)
23
+ repo.push_to_hub()
24
+ st.success("Model uploaded to Hugging Face Hub successfully!")
25
+ else:
26
+ st.error("Model directory does not exist. Please make sure the model is fine-tuned first.")
27
+
28
+ # Section for using the model in the app
29
+ st.header("Ask Me Any Coding Question!")
30
+
31
+ # Load model and tokenizer (either from local directory or Hugging Face Hub)
32
+ model_name = repo_name # Use the repo name if the model is on Hugging Face Hub, else use local dir
33
+
34
+ # Check if the model is uploaded to Hugging Face Hub
35
+ if os.path.exists(model_dir):
36
+ # Load the model and tokenizer from local directory
37
+ model = AutoModelForCausalLM.from_pretrained(model_dir, load_in_8bit=True)
38
+ tokenizer = AutoTokenizer.from_pretrained(model_dir)
39
+ else:
40
+ # Load the model from Hugging Face Hub if it's not found locally
41
+ model = AutoModelForCausalLM.from_pretrained(model_name)
42
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
43
+
44
+ # User input: Coding question
45
  question = st.text_input("Enter your coding question:")
46
 
47
  if question:
 
48
  input_text = f"### Question:\n{question}\n### Answer:"
49
  inputs = tokenizer(input_text, return_tensors="pt")
50
 
51
+ with st.spinner("Processing..."):
52
+ output = model.generate(**inputs, max_length=200, num_return_sequences=1)
53
+ answer = tokenizer.decode(output[0], skip_special_tokens=True)
 
 
54
 
 
55
  st.write(answer)