Avinash109 commited on
Commit
7e2ed99
·
verified ·
1 Parent(s): c2013e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -59
app.py CHANGED
@@ -1,60 +1,50 @@
1
  import pandas as pd
2
- import gradio as gr
3
- from transformers import pipeline
4
-
5
- # Load the already uploaded BANKNIFTY option chain data
6
- file_path = '/mnt/data/BANKNIFTY_OPTION_CHAIN_data.csv'
7
-
8
- def load_data():
9
- try:
10
- # Load the CSV file from the file path
11
- data = pd.read_csv(file_path)
12
- return data
13
- except Exception as e:
14
- return f"Error loading data: {e}"
15
-
16
- # Analyze the data using a Hugging Face model
17
- def analyze_data(question, model_name="impira/layoutlm-document-qa"):
18
- # Load the data
19
- data = load_data()
20
- if isinstance(data, str): # If error string is returned
21
- return data
22
-
23
- # Load the Hugging Face LayoutLM pipeline for document question answering
24
- try:
25
- question_answerer = pipeline("document-question-answering", model=model_name)
26
-
27
- # Convert DataFrame to JSON-like format for the model
28
- context = data.to_json(orient='records')
29
-
30
- # Ask the question and get the answer
31
- result = question_answerer(question=question, context=context)
32
- return result
33
- except Exception as e:
34
- return f"Error analyzing data: {e}"
35
-
36
- # Gradio interface definition
37
- def gradio_interface():
38
- # Define input fields
39
- question_input = gr.inputs.Textbox(lines=2, placeholder="Ask a question about the data", label="Question")
40
-
41
- # Output field
42
- output_text = gr.outputs.Textbox(label="Answer")
43
-
44
- # Create the Gradio Interface
45
- interface = gr.Interface(
46
- fn=analyze_data,
47
- inputs=question_input,
48
- outputs=output_text,
49
- title="BANKNIFTY Option Chain Analyzer",
50
- description="Ask a question to extract insights from the BANKNIFTY option chain data.",
51
- allow_flagging="never"
52
- )
53
-
54
- return interface
55
-
56
- # Launch Gradio app
57
- if __name__ == "__main__":
58
- # Start the Gradio interface
59
- app = gradio_interface()
60
- app.launch()
 
1
  import pandas as pd
2
+ from transformers import LLaMAForSequenceClassification, LLaMATokenizer
3
+
4
+ # Load the data
5
+ data = pd.read_csv('BANKNIFTY_OPTION_CHAIN_data.csv')
6
+
7
+ # Preprocess the data
8
+ tokenizer = LLaMATokenizer.from_pretrained('llama-2-7b')
9
+ model = LLaMAForSequenceClassification.from_pretrained('llama-2-7b', num_labels=2)
10
+
11
+ # Fine-tune the model on the dataset
12
+ train_texts, val_texts, train_labels, val_labels = train_test_split(data['text'], data['label'], test_size=0.2, random_state=42)
13
+
14
+ train_encodings = tokenizer(train_texts, truncation=True, padding=True)
15
+ val_encodings = tokenizer(val_texts, truncation=True, padding=True)
16
+
17
+ train_dataset = Dataset(train_encodings, train_labels)
18
+ val_dataset = Dataset(val_encodings, val_labels)
19
+
20
+ training_args = TrainingArguments(
21
+ output_dir='./results', # output directory
22
+ num_train_epochs=3, # total # of training epochs
23
+ per_device_train_batch_size=16, # batch size per device during training
24
+ per_device_eval_batch_size=64, # batch size for evaluation
25
+ warmup_steps=500, # number of warmup steps for learning rate scheduler
26
+ weight_decay=0.01, # strength of weight decay
27
+ logging_dir='./logs', # directory for storing logs
28
+ )
29
+
30
+ trainer = Trainer(
31
+ model=model, # the instantiated model
32
+ args=training_args, # training arguments
33
+ train_dataset=train_dataset, # training dataset
34
+ eval_dataset=val_dataset # evaluation dataset
35
+ )
36
+
37
+ trainer.train()
38
+
39
+ # Use the fine-tuned model to generate strategies
40
+ def generate_strategies(data):
41
+ inputs = tokenizer(data['text'], return_tensors='pt')
42
+ outputs = model(**inputs)
43
+ logits = outputs.logits
44
+ strategies = torch.argmax(logits, dim=1)
45
+ return strategies
46
+
47
+ strategies = generate_strategies(data)
48
+
49
+ # Print the strategies
50
+ print(strategies)