Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
# -*- coding: utf-8 -*-
|
2 |
-
"""
|
3 |
|
4 |
Automatically generated by Colab.
|
5 |
|
@@ -7,25 +7,10 @@ Original file is located at
|
|
7 |
https://colab.research.google.com/drive/10LVNp23wimf3FbDIurOgtyckwjSIo3lK
|
8 |
"""
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
pip install gradio speechrecognition
|
13 |
-
|
14 |
-
pip install pytesseract
|
15 |
-
|
16 |
-
pip install gradio
|
17 |
-
|
18 |
import cv2 # For image processing with OpenCV
|
19 |
import pytesseract # For Optical Character Recognition (OCR) on receipts
|
20 |
import gradio as gr # For creating the Gradio interface
|
21 |
-
|
22 |
-
apt update
|
23 |
-
apt install -y tesseract-ocr
|
24 |
-
|
25 |
-
pip install langchain langchain-community langchain-core transformers
|
26 |
-
|
27 |
-
pip install bitsandbytes accelerate
|
28 |
-
|
29 |
from torch import cuda, bfloat16
|
30 |
import transformers
|
31 |
from transformers import AutoTokenizer
|
@@ -33,10 +18,12 @@ import torch
|
|
33 |
from langchain.llms import HuggingFacePipeline
|
34 |
from langchain.chains import RetrievalQA
|
35 |
from langchain.vectorstores import chroma
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
device= f'cuda:{cuda.current_device()}' if cuda.is_available() else 'cpu'
|
40 |
bnb_config = transformers.BitsAndBytesConfig(
|
41 |
load_in_4bit=True,
|
42 |
bnb_4bit_quant_type='nf4',
|
@@ -45,13 +32,13 @@ bnb_config = transformers.BitsAndBytesConfig(
|
|
45 |
)
|
46 |
print(device)
|
47 |
|
48 |
-
|
49 |
os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
|
50 |
-
model_config =
|
51 |
model_id,
|
52 |
trust_remote_code=True,
|
53 |
max_new_tokens=1024
|
54 |
-
|
55 |
model = transformers.AutoModelForCausalLM.from_pretrained(
|
56 |
model_id,
|
57 |
trust_remote_code=True,
|
@@ -61,6 +48,7 @@ model = transformers.AutoModelForCausalLM.from_pretrained(
|
|
61 |
)
|
62 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
63 |
|
|
|
64 |
query_pipeline = transformers.pipeline(
|
65 |
'text-generation',
|
66 |
model=model,
|
@@ -71,11 +59,12 @@ query_pipeline = transformers.pipeline(
|
|
71 |
device_map="auto"
|
72 |
)
|
73 |
|
|
|
74 |
from IPython.display import display, Markdown
|
75 |
def colorize_text(text):
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
|
80 |
llm = HuggingFacePipeline(pipeline=query_pipeline)
|
81 |
|
@@ -91,7 +80,7 @@ Groceries: $496.0, Supplies: $454.42, Food: $341.69, Electronics: $351.92,
|
|
91 |
Home Shopping: $235.68, Others: $253.45
|
92 |
"""
|
93 |
|
94 |
-
question = "Based on this data, can
|
95 |
|
96 |
# Combine structured data into prompt
|
97 |
prompt = f"{user_profile}\n\nQuestion: {question}"
|
@@ -103,8 +92,7 @@ response = llm(prompt=prompt)
|
|
103 |
full_response = f"**Question:** {question}\n\n**Answer:** {response}"
|
104 |
display(Markdown(colorize_text(full_response)))
|
105 |
|
106 |
-
#
|
107 |
-
import speech_recognition as sr
|
108 |
def scan_receipt(image):
|
109 |
try:
|
110 |
img_orig = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
@@ -114,7 +102,7 @@ def scan_receipt(image):
|
|
114 |
except Exception as e:
|
115 |
return f"An error occurred: {str(e)}"
|
116 |
|
117 |
-
#
|
118 |
def record_expense(audio_path):
|
119 |
recognizer = sr.Recognizer()
|
120 |
with sr.AudioFile(audio_path) as source:
|
@@ -213,4 +201,4 @@ with gr.Blocks() as iface:
|
|
213 |
)
|
214 |
|
215 |
# Launch the app
|
216 |
-
iface.launch()
|
|
|
1 |
# -*- coding: utf-8 -*-
|
2 |
+
"""saivv_prototype
|
3 |
|
4 |
Automatically generated by Colab.
|
5 |
|
|
|
7 |
https://colab.research.google.com/drive/10LVNp23wimf3FbDIurOgtyckwjSIo3lK
|
8 |
"""
|
9 |
|
10 |
+
# Import necessary libraries
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
import cv2 # For image processing with OpenCV
|
12 |
import pytesseract # For Optical Character Recognition (OCR) on receipts
|
13 |
import gradio as gr # For creating the Gradio interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
from torch import cuda, bfloat16
|
15 |
import transformers
|
16 |
from transformers import AutoTokenizer
|
|
|
18 |
from langchain.llms import HuggingFacePipeline
|
19 |
from langchain.chains import RetrievalQA
|
20 |
from langchain.vectorstores import chroma
|
21 |
+
import os
|
22 |
+
import speech_recognition as sr # For voice recording
|
23 |
|
24 |
+
# Model configuration
|
25 |
+
model_id = 'HuggingFaceH4/zephyr-7b-beta'
|
26 |
+
device = f'cuda:{cuda.current_device()}' if cuda.is_available() else 'cpu'
|
27 |
bnb_config = transformers.BitsAndBytesConfig(
|
28 |
load_in_4bit=True,
|
29 |
bnb_4bit_quant_type='nf4',
|
|
|
32 |
)
|
33 |
print(device)
|
34 |
|
35 |
+
# Set CUDA environment variable
|
36 |
os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
|
37 |
+
model_config = transformers.AutoConfig.from_pretrained(
|
38 |
model_id,
|
39 |
trust_remote_code=True,
|
40 |
max_new_tokens=1024
|
41 |
+
)
|
42 |
model = transformers.AutoModelForCausalLM.from_pretrained(
|
43 |
model_id,
|
44 |
trust_remote_code=True,
|
|
|
48 |
)
|
49 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
50 |
|
51 |
+
# Setup pipeline
|
52 |
query_pipeline = transformers.pipeline(
|
53 |
'text-generation',
|
54 |
model=model,
|
|
|
59 |
device_map="auto"
|
60 |
)
|
61 |
|
62 |
+
# Function to format text with colors
|
63 |
from IPython.display import display, Markdown
|
64 |
def colorize_text(text):
|
65 |
+
for word, color in zip(["Reasoning", "Question", "Answer", "Total time"], ["blue", "red", "green", "magenta"]):
|
66 |
+
text = text.replace(f"{word}:", f"\n\n**<font color='{color}'>{word}:</font>**")
|
67 |
+
return text
|
68 |
|
69 |
llm = HuggingFacePipeline(pipeline=query_pipeline)
|
70 |
|
|
|
80 |
Home Shopping: $235.68, Others: $253.45
|
81 |
"""
|
82 |
|
83 |
+
question = "Based on this data, can I buy a Lamborghini?"
|
84 |
|
85 |
# Combine structured data into prompt
|
86 |
prompt = f"{user_profile}\n\nQuestion: {question}"
|
|
|
92 |
full_response = f"**Question:** {question}\n\n**Answer:** {response}"
|
93 |
display(Markdown(colorize_text(full_response)))
|
94 |
|
95 |
+
# Function for receipt scanning with OCR
|
|
|
96 |
def scan_receipt(image):
|
97 |
try:
|
98 |
img_orig = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
|
|
102 |
except Exception as e:
|
103 |
return f"An error occurred: {str(e)}"
|
104 |
|
105 |
+
# Function for voice recording
|
106 |
def record_expense(audio_path):
|
107 |
recognizer = sr.Recognizer()
|
108 |
with sr.AudioFile(audio_path) as source:
|
|
|
201 |
)
|
202 |
|
203 |
# Launch the app
|
204 |
+
iface.launch()
|