SumanthKarnati commited on
Commit
8f9e99e
·
1 Parent(s): 8f99068

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -13
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import nltk
2
- from transformers import VisionEncoderDecoderModel, AutoTokenizer, ViTImageProcessor, GPTNeoForCausalLM, GPT2Tokenizer
3
  import torch
4
  from PIL import Image
5
  import streamlit as st
@@ -7,6 +7,7 @@ from nltk.corpus import stopwords
7
  import os
8
  from io import BytesIO
9
 
 
10
  os.system('pip install --upgrade transformers')
11
  # os.system('pip install nltk')
12
  nltk.download('stopwords')
@@ -24,9 +25,8 @@ feature_extractor = ViTImageProcessor.from_pretrained(
24
  tokenizer = AutoTokenizer.from_pretrained(
25
  'nlpconnect/vit-gpt2-image-captioning')
26
 
27
- # Load GPT-2 model and tokenizer
28
- gpt2_model = GPTNeoForCausalLM.from_pretrained('EleutherAI/gpt-neo-1.3-125')
29
- gpt2_tokenizer = GPT2Tokenizer.from_pretrained('EleutherAI/gpt-neo-1.3-125')
30
 
31
  # Device configuration
32
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
@@ -115,16 +115,11 @@ def main():
115
  # Prepare the prompt
116
  prompt = f"You are a knowledgeable assistant that provides nutritional advice based on a list of ingredients. The identified ingredients are: {preds_str}. Note that some ingredients may not make sense, so use the ones that do. Can you provide a nutritional analysis and suggestions for improvement?"
117
 
118
- # Encode and add special tokens
119
- input_ids = gpt2_tokenizer.encode(prompt, return_tensors='pt')
120
-
121
- # Generate a sequence of text
122
- output = gpt2_model.generate(
123
- input_ids, max_length=200, temperature=0.7, pad_token_id=gpt2_tokenizer.eos_token_id)
124
 
125
- # Decode the output
126
- suggestions = gpt2_tokenizer.decode(
127
- output[:, input_ids.shape[-1]:][0], clean_up_tokenization_spaces=True)
128
 
129
  st.subheader("Nutritional Analysis and Suggestions:")
130
  st.write(suggestions)
 
1
  import nltk
2
+ from transformers import VisionEncoderDecoderModel, AutoTokenizer, ViTImageProcessor, pipeline
3
  import torch
4
  from PIL import Image
5
  import streamlit as st
 
7
  import os
8
  from io import BytesIO
9
 
10
+
11
  os.system('pip install --upgrade transformers')
12
  # os.system('pip install nltk')
13
  nltk.download('stopwords')
 
25
  tokenizer = AutoTokenizer.from_pretrained(
26
  'nlpconnect/vit-gpt2-image-captioning')
27
 
28
+ # Set up text generation pipeline
29
+ generator = pipeline('text-generation', model='EleutherAI/gpt-neo-2.7B')
 
30
 
31
  # Device configuration
32
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 
115
  # Prepare the prompt
116
  prompt = f"You are a knowledgeable assistant that provides nutritional advice based on a list of ingredients. The identified ingredients are: {preds_str}. Note that some ingredients may not make sense, so use the ones that do. Can you provide a nutritional analysis and suggestions for improvement?"
117
 
118
+ # Generate a sequence of text
119
+ suggestions = generator(prompt, do_sample=True, min_length=200)
 
 
 
 
120
 
121
+ # Extract the generated text
122
+ suggestions = suggestions[0]['generated_text'][len(prompt):]
 
123
 
124
  st.subheader("Nutritional Analysis and Suggestions:")
125
  st.write(suggestions)