Canstralian commited on
Commit
69f088a
·
verified ·
1 Parent(s): 8faaaac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -114
app.py CHANGED
@@ -1,118 +1,25 @@
1
- import streamlit as st
2
- from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
3
- from typing import List, Dict
4
  import os
5
 
6
- # Initialize the Hugging Face pipeline (ensure to use a valid model)
7
- model_name = "your_huggingface_model_name" # Ensure to use a valid model
8
- tokenizer = AutoTokenizer.from_pretrained(model_name)
 
9
  try:
10
- model = AutoModelForCausalLM.from_pretrained(model_name)
11
- generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
 
12
  except Exception as e:
13
- st.error(f"Error initializing the model '{model_name}': {e}")
14
- generator = None # Set generator to None if model fails to load
15
-
16
- # Function to generate OSINT results
17
- def generate_osint_results(prompt: str, history: List[Dict[str, str]]) -> List[str]:
18
- """
19
- Simulates generating OSINT-based results from the user's input.
20
- Args:
21
- prompt (str): The user's input to the simulator.
22
- history (List[Dict]): The user's message history with timestamps.
23
- Returns:
24
- List[str]: A list of OSINT responses from the AI.
25
- """
26
- # Validate inputs
27
- if not prompt.strip():
28
- return ["Error: Prompt cannot be empty."]
29
- if not isinstance(history, list) or not all(isinstance(h, dict) for h in history):
30
- return ["Error: History must be a list of dictionaries."]
31
-
32
- # Prepare messages for the AI
33
- messages = [{"role": "system", "content": f"Responding to OSINT prompt: {prompt}"}]
34
- for val in history:
35
- if "user" in val:
36
- messages.append({"role": "user", "content": val["user"]})
37
- if "assistant" in val:
38
- messages.append({"role": "assistant", "content": val["assistant"]})
39
-
40
- # Append the current user prompt
41
- messages.append({"role": "user", "content": prompt})
42
-
43
- # Generate a response using the Hugging Face model
44
- if generator:
45
- try:
46
- response = generator(messages[-1]["content"], max_length=100, num_return_sequences=1)
47
- return [response[0]["generated_text"]]
48
- except Exception as e:
49
- return [f"Error generating response: {e}"]
50
- else:
51
- return ["Error: Model initialization failed."]
52
-
53
- # Function for fine-tuning the model with the uploaded dataset
54
- def fine_tune_model(dataset: str) -> str:
55
- """
56
- Fine-tunes the model using the uploaded dataset.
57
- Args:
58
- dataset (str): The path to the dataset for fine-tuning.
59
- Returns:
60
- str: A message indicating whether fine-tuning was successful or failed.
61
- """
62
- try:
63
- # Process the dataset (dummy processing for illustration)
64
- with open(dataset, "r") as file:
65
- data = file.readlines()
66
-
67
- # Simulate fine-tuning with the provided dataset
68
- # Here, you would use the data to fine-tune the model
69
- # For this example, we're not actually fine-tuning the model.
70
- model.save_pretrained("./fine_tuned_model")
71
- return "Model fine-tuned successfully!"
72
- except Exception as e:
73
- return f"Error fine-tuning the model: {e}"
74
-
75
- # Streamlit app interface
76
- st.title("OSINT Tool")
77
- st.write("This tool generates OSINT-based results and allows you to fine-tune the model with custom datasets.")
78
-
79
- # User input for prompt and message history
80
- prompt = st.text_area("Enter your OSINT prompt here...", placeholder="Type your prompt here...")
81
-
82
- # Initialize session state for message history
83
- if "history" not in st.session_state:
84
- st.session_state.history = []
85
-
86
- # Display past conversation
87
- st.write("### Message History:")
88
- for msg in st.session_state.history:
89
- st.write(f"**User**: {msg['user']}")
90
- st.write(f"**Assistant**: {msg['assistant']}")
91
-
92
- # Fine-tuning functionality
93
- dataset_file = st.file_uploader("Upload a dataset for fine-tuning", type=["txt"])
94
-
95
- if dataset_file is not None:
96
- # Save the uploaded file
97
- dataset_path = os.path.join("uploads", dataset_file.name)
98
- os.makedirs("uploads", exist_ok=True)
99
- with open(dataset_path, "wb") as f:
100
- f.write(dataset_file.read())
101
-
102
- # Fine-tune the model
103
- fine_tuning_status = fine_tune_model(dataset_path)
104
- st.success(fine_tuning_status)
105
-
106
- # Generate OSINT response when prompt is entered
107
- if st.button("Generate OSINT Results"):
108
- if prompt:
109
- response = generate_osint_results(prompt, st.session_state.history)
110
- st.session_state.history.append({"user": prompt, "assistant": response[0]})
111
- st.write("### Generated OSINT Result:")
112
- st.write(response[0])
113
- else:
114
- st.error("Please enter a prompt.")
115
-
116
- # Optionally save fine-tuned model
117
- if os.path.exists("./fine_tuned_model"):
118
- st.write("The model has been fine-tuned and saved as `fine_tuned_model`.")
 
1
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
 
 
2
  import os
3
 
4
+ # Define the model name (replace with your actual model name)
5
+ model_name = "huggingface/transformers" # Example model name
6
+
7
+ # Load the tokenizer and model
8
  try:
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
10
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
11
+ print("Model and tokenizer loaded successfully!")
12
  except Exception as e:
13
+ print(f"Error loading model: {e}")
14
+
15
+ # Add your app logic here (e.g., for inference, etc.)
16
+ def predict(text):
17
+ inputs = tokenizer(text, return_tensors="pt")
18
+ outputs = model(**inputs)
19
+ return outputs
20
+
21
+ # Example usage
22
+ if __name__ == "__main__":
23
+ test_text = "Hello, world!"
24
+ result = predict(test_text)
25
+ print(result)