anirudh248 commited on
Commit
e046560
·
verified ·
1 Parent(s): 276bf2f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -7
app.py CHANGED
@@ -1,8 +1,8 @@
 
1
  import gradio as gr
2
  import torch
3
- from langchain_community.embeddings import HuggingFaceEmbeddings
4
  from langchain_community.vectorstores import FAISS
5
- from langchain_huggingface import HuggingFacePipeline
6
  from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
7
  from langchain_classic.chains import create_retrieval_chain
8
  from langchain_classic.chains.combine_documents import create_stuff_documents_chain
@@ -28,7 +28,7 @@ retriever = vectorstore.as_retriever()
28
  print("Loading Model...")
29
  model_id = "anirudh248/llama3-upf-generator"
30
 
31
- # Load in 4-bit to fit inside a T4 GPU Space
32
  bnb_config = BitsAndBytesConfig(
33
  load_in_4bit=True,
34
  bnb_4bit_compute_dtype=torch.float16
@@ -74,13 +74,16 @@ prompt = PromptTemplate.from_template(prompt_template)
74
  document_chain = create_stuff_documents_chain(llm, prompt)
75
  rag_chain = create_retrieval_chain(retriever, document_chain)
76
 
 
 
 
 
 
 
77
  def generate_upf_code(power_intent_description):
78
  result = rag_chain.invoke({"input": power_intent_description})
79
  return result['answer'].strip()
80
 
81
- # ==========================================
82
- # 4. Gradio UI
83
- # ==========================================
84
  def user_interaction(user_message, history):
85
  history = history or []
86
  response = generate_upf_code(user_message)
@@ -95,7 +98,6 @@ def user_interaction(user_message, history):
95
  with gr.Blocks() as interface:
96
  gr.Markdown("# ⚡ UPF Code Generator with Llama 3 & RAG")
97
 
98
- # Removed `type="messages"` parameter to fix TypeError
99
  chatbot = gr.Chatbot(label="Chat History", elem_id="chatbot")
100
 
101
  with gr.Row():
 
1
+ import spaces
2
  import gradio as gr
3
  import torch
4
+ from langchain_huggingface import HuggingFaceEmbeddings, HuggingFacePipeline
5
  from langchain_community.vectorstores import FAISS
 
6
  from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
7
  from langchain_classic.chains import create_retrieval_chain
8
  from langchain_classic.chains.combine_documents import create_stuff_documents_chain
 
28
  print("Loading Model...")
29
  model_id = "anirudh248/llama3-upf-generator"
30
 
31
+ # Load in 4-bit to fit perfectly inside a GPU Space
32
  bnb_config = BitsAndBytesConfig(
33
  load_in_4bit=True,
34
  bnb_4bit_compute_dtype=torch.float16
 
74
  document_chain = create_stuff_documents_chain(llm, prompt)
75
  rag_chain = create_retrieval_chain(retriever, document_chain)
76
 
77
+
78
+ # ==========================================
79
+ # 4. Gradio UI & Inference
80
+ # ==========================================
81
+
82
+ @spaces.GPU(duration=120)
83
  def generate_upf_code(power_intent_description):
84
  result = rag_chain.invoke({"input": power_intent_description})
85
  return result['answer'].strip()
86
 
 
 
 
87
  def user_interaction(user_message, history):
88
  history = history or []
89
  response = generate_upf_code(user_message)
 
98
  with gr.Blocks() as interface:
99
  gr.Markdown("# ⚡ UPF Code Generator with Llama 3 & RAG")
100
 
 
101
  chatbot = gr.Chatbot(label="Chat History", elem_id="chatbot")
102
 
103
  with gr.Row():