Yoxas commited on
Commit
5f7eaa3
·
verified ·
1 Parent(s): 5780f69

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -79
app.py CHANGED
@@ -1,87 +1,15 @@
1
  import gradio as gr
2
- import pandas as pd
3
- import numpy as np
4
- from transformers import pipeline, BertTokenizer, BertModel
5
- import faiss
6
- import torch
7
- import json
8
- import spaces
9
 
10
- # Load CSV data
11
- data = pd.read_csv('RBDx10kstats.csv')
12
 
13
- # Function to safely convert JSON strings to numpy arrays
14
- def safe_json_loads(x):
15
- try:
16
- return np.array(json.loads(x), dtype=np.float32) # Ensure the array is of type float32
17
- except json.JSONDecodeError as e:
18
- print(f"Error decoding JSON: {e}")
19
- return np.array([], dtype=np.float32) # Return an empty array or handle it as appropriate
20
-
21
- # Apply the safe_json_loads function to the embedding column
22
- data['embedding'] = data['embedding'].apply(safe_json_loads)
23
-
24
- # Filter out any rows with empty embeddings
25
- data = data[data['embedding'].apply(lambda x: x.size > 0)]
26
-
27
- # Initialize FAISS index
28
- dimension = len(data['embedding'].iloc[0])
29
- res = faiss.StandardGpuResources() # use a single GPU
30
-
31
- # Create FAISS index
32
- if faiss.get_num_gpus() > 0:
33
- gpu_index = faiss.IndexFlatL2(dimension)
34
- gpu_index = faiss.index_cpu_to_gpu(res, 0, gpu_index) # move to GPU
35
- else:
36
- gpu_index = faiss.IndexFlatL2(dimension) # fall back to CPU
37
-
38
- # Ensure embeddings are stacked as float32
39
- embeddings = np.vstack(data['embedding'].values).astype(np.float32)
40
- gpu_index.add(embeddings)
41
-
42
- # Check if GPU is available
43
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
44
-
45
- # Load QA model
46
- qa_model = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad", device=0 if torch.cuda.is_available() else -1)
47
-
48
- # Load BERT model and tokenizer
49
- tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
50
- model = BertModel.from_pretrained('bert-base-uncased').to(device)
51
-
52
- # Function to embed the question using BERT
53
- def embed_question(question, model, tokenizer):
54
- inputs = tokenizer(question, return_tensors='pt').to(device)
55
- with torch.no_grad():
56
- outputs = model(**inputs)
57
- return outputs.last_hidden_state.mean(dim=1).cpu().numpy().astype(np.float32)
58
-
59
- # Function to retrieve the relevant document and generate a response
60
- @spaces.GPU(duration=120)
61
- def retrieve_and_generate(question):
62
- # Embed the question
63
- question_embedding = embed_question(question, model, tokenizer)
64
-
65
- # Search in FAISS index
66
- _, indices = gpu_index.search(question_embedding, k=1)
67
-
68
- # Retrieve the most relevant document
69
- relevant_doc = data.iloc[indices[0][0]]
70
-
71
- # Use the QA model to generate the answer
72
- context = relevant_doc['Abstract']
73
- response = qa_model(question=question, context=context)
74
-
75
- return response['answer']
76
-
77
- # Create a Gradio interface
78
  interface = gr.Interface(
79
- fn=retrieve_and_generate,
80
- inputs=gr.Textbox(lines=2, placeholder="Ask a question about the documents..."),
81
  outputs="text",
82
- title="RAG Chatbot",
83
- description="Ask questions about the documents in the CSV file."
84
  )
85
 
86
  # Launch the Gradio app
87
- interface.launch()
 
1
  import gradio as gr
 
 
 
 
 
 
 
2
 
3
+ def simple_greeting(name):
4
+ return f"Hello {name}!"
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  interface = gr.Interface(
7
+ fn=simple_greeting,
8
+ inputs=gr.Textbox(lines=2, placeholder="Enter your name..."),
9
  outputs="text",
10
+ title="Simple Greeting",
11
+ description="Test Gradio Interface"
12
  )
13
 
14
  # Launch the Gradio app
15
+ interface.launch()