mohAhmad commited on
Commit
5578f37
·
verified ·
1 Parent(s): 75e78ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -35
app.py CHANGED
@@ -1,52 +1,51 @@
1
  import streamlit as st
2
  import pandas as pd
3
  import matplotlib.pyplot as plt
4
- from transformers import TFTForConditionalGeneration, TFTTokenizer
5
- import torch
6
-
7
- # Function to load data and model
8
- @st.cache_data
9
- def load_data(file):
10
- data = pd.read_csv(file)
11
- return data
12
-
13
- # Function to predict using the model
14
- def predict_earthquake_positions(data, model, tokenizer):
15
- inputs = tokenizer(data.to_dict(orient='list'), return_tensors="pt", padding=True, truncation=True)
16
- with torch.no_grad():
17
- outputs = model.generate(inputs['input_ids'], num_beams=5, early_stopping=True)
18
- return outputs
19
-
20
- # Load Hugging Face model and tokenizer
21
  @st.cache_resource
22
  def load_model():
23
- model = TFTForConditionalGeneration.from_pretrained("huggingface/tft")
24
- tokenizer = TFTTokenizer.from_pretrained("huggingface/tft")
25
- return model, tokenizer
 
 
26
 
27
- # Streamlit App
28
- st.title('Earthquake Detection Prediction App')
29
 
30
- uploaded_file = st.file_uploader("Upload your CSV file", type=["csv"])
 
31
 
32
  if uploaded_file is not None:
33
- data = load_data(uploaded_file)
 
34
 
35
  # Display the data
36
- st.subheader("Uploaded Data")
37
- st.write(data)
38
 
39
- # Load the model and tokenizer
40
- model, tokenizer = load_model()
 
41
 
42
- # Make predictions
43
- st.subheader("Predictions")
44
- predictions = predict_earthquake_positions(data, model, tokenizer)
45
 
46
- # Plotting the predictions
47
- st.subheader("Earthquake Prediction Plot")
48
  fig, ax = plt.subplots()
49
- ax.plot(data['x'], data['prediction'], label="Predicted Earthquake Position", color='green')
50
- ax.axvline(x=predictions, color='red', linestyle='--', label='Predicted Earthquake Start')
51
  ax.legend()
52
  st.pyplot(fig)
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import pandas as pd
3
  import matplotlib.pyplot as plt
4
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
5
+
6
+ # Load the model and tokenizer from Hugging Face
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  @st.cache_resource
8
  def load_model():
9
+ tokenizer = AutoTokenizer.from_pretrained("t5-small") # Small T5 model for demo
10
+ model = AutoModelForSeq2SeqLM.from_pretrained("t5-small")
11
+ return tokenizer, model
12
+
13
+ tokenizer, model = load_model()
14
 
15
+ # Streamlit app
16
+ st.title("Seismic Event Prediction App")
17
 
18
+ # File upload section
19
+ uploaded_file = st.file_uploader("Upload CSV File", type=["csv"])
20
 
21
  if uploaded_file is not None:
22
+ # Load CSV data
23
+ data = pd.read_csv(uploaded_file)
24
 
25
  # Display the data
26
+ st.write("## Uploaded Data")
27
+ st.dataframe(data)
28
 
29
+ # Input slider for choosing an example (index between 0 and N-1)
30
+ st.write("## Select an example to visualize:")
31
+ idx = st.slider("Choose an index", 0, len(data) - 1, 0)
32
 
33
+ # Prediction and plotting
34
+ st.write("### Selected example:", idx)
35
+ st.write(data.iloc[idx])
36
 
37
+ # Plot the predictions
 
38
  fig, ax = plt.subplots()
39
+ ax.plot(data['x'], label="X-axis data", color="blue")
40
+ ax.axvline(x=data.iloc[idx]['prediction'], color="red", label="Predicted Earthquake")
41
  ax.legend()
42
  st.pyplot(fig)
43
+
44
+ # Use the Hugging Face model to generate a simple summary or prediction based on the selected row
45
+ input_text = f"Predict seismic event for index {idx}."
46
+ inputs = tokenizer.encode(input_text, return_tensors="pt")
47
+ outputs = model.generate(inputs, max_length=50, num_beams=4, early_stopping=True)
48
+ generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
49
+
50
+ st.write("### Model Prediction:")
51
+ st.write(generated_text)