JoannaKOKO commited on
Commit
af9fb37
·
verified ·
1 Parent(s): a314f0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -2,9 +2,11 @@ import os
2
  from huggingface_hub import login
3
  import gradio as gr
4
  from PIL import Image
5
- from transformers import AutoProcessor, AutoModelForImageTextToText, pipeline
6
  import torch
7
  import spaces
 
 
8
 
9
  # Function to process vision information
10
  def process_vision_info(messages: list[dict]) -> list[Image.Image]:
@@ -33,11 +35,10 @@ def load_image_model():
33
 
34
  # Load text model on CPU
35
  def load_text_model():
36
- return pipeline(
37
- "text-generation",
38
- model="tarotscientist/llama-2-7b-tarotreader",
39
- device=-1 # Force CPU
40
- )
41
 
42
  # Generate card description with ZeroGPU
43
  @spaces.GPU
@@ -76,7 +77,7 @@ def generate_description(sample, model, processor):
76
 
77
  # Generate tarot interpretation with ZeroGPU
78
  @spaces.GPU
79
- def generate_interpretation(question, cards, model):
80
  prompt = f"""Analyze this tarot reading for the question: {question}
81
 
82
  Cards:
@@ -89,8 +90,9 @@ Provide a professional interpretation covering:
89
  - Practical advice
90
  - Potential outcomes"""
91
  # Use GPU for this inference call
92
- response = model(prompt, max_length=1000, temperature=0.8, top_p=0.95)[0]['generated_text']
93
- return response
 
94
 
95
  def main():
96
  """
@@ -105,7 +107,7 @@ def main():
105
 
106
  # Load models on CPU
107
  image_processor, image_model = load_image_model()
108
- text_model = load_text_model()
109
 
110
  # Define the tarot processing function
111
  def process_tarot(question, reason_img, result_img, recommendation_img):
@@ -130,7 +132,7 @@ def main():
130
  output = "### Identifing Card Name...\n"
131
 
132
  # Generate the full interpretation using GPU
133
- interpretation = generate_interpretation(question, cards, text_model)
134
 
135
  # Format the output
136
  output += "### Card Analysis\n"
 
2
  from huggingface_hub import login
3
  import gradio as gr
4
  from PIL import Image
5
+ from transformers import AutoProcessor, AutoModelForImageTextToText
6
  import torch
7
  import spaces
8
+ from peft import PeftModel
9
+ from transformers import AutoModelForCausalLM
10
 
11
  # Function to process vision information
12
  def process_vision_info(messages: list[dict]) -> list[Image.Image]:
 
35
 
36
  # Load text model on CPU
37
  def load_text_model():
38
+ base_model = AutoModelForCausalLM.from_pretrained("google/gemma-2-2b")
39
+ model = PeftModel.from_pretrained(base_model, "soonbob/gemma-2-2b-tarot")
40
+ tokenizer = AutoTokenizer.from_pretrained("google/gemma-2-2b")
41
+ return model, tokenizer
 
42
 
43
  # Generate card description with ZeroGPU
44
  @spaces.GPU
 
77
 
78
  # Generate tarot interpretation with ZeroGPU
79
  @spaces.GPU
80
+ def generate_interpretation(question, cards, model, tokenizer):
81
  prompt = f"""Analyze this tarot reading for the question: {question}
82
 
83
  Cards:
 
90
  - Practical advice
91
  - Potential outcomes"""
92
  # Use GPU for this inference call
93
+ input_ids = tokenizer(prompt, return_tensors="pt").to("cuda")
94
+ outputs = model.generate(**input_ids, max_new_tokens=32)
95
+ return (tokenizer.decode(outputs[0]))
96
 
97
  def main():
98
  """
 
107
 
108
  # Load models on CPU
109
  image_processor, image_model = load_image_model()
110
+ text_model, text_tokenizer = load_text_model()
111
 
112
  # Define the tarot processing function
113
  def process_tarot(question, reason_img, result_img, recommendation_img):
 
132
  output = "### Identifing Card Name...\n"
133
 
134
  # Generate the full interpretation using GPU
135
+ interpretation = generate_interpretation(question, cards, text_model, text_tokenizer)
136
 
137
  # Format the output
138
  output += "### Card Analysis\n"