Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import transformers
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, DataCollatorForSeq2Seq
|
3 |
+
from datasets import load_dataset, load_from_disk
|
4 |
+
from evaluate import load
|
5 |
+
import torch
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Use a pipeline as a high-level helper
|
9 |
+
from transformers import pipeline
|
10 |
+
|
11 |
+
pipe = pipeline("text-generation", model="openaccess-ai-collective/minotaur-15b")
|
12 |
+
# Load model directly
|
13 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
14 |
+
|
15 |
+
tokenizer = AutoTokenizer.from_pretrained("openaccess-ai-collective/minotaur-15b")
|
16 |
+
model = AutoModelForCausalLM.from_pretrained("openaccess-ai-collective/minotaur-15b")
|
17 |
+
model_id = "your_model_id" # Replace with your model ID
|
18 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
19 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_id)
|
20 |
+
data_collator = DataCollatorForSeq2Seq(tokenizer, model=model)
|
21 |
+
|
22 |
+
def generate_answer(question, file_path):
|
23 |
+
if os.path.exists(file_path):
|
24 |
+
# Load data from file
|
25 |
+
if file_path.endswith(".csv"):
|
26 |
+
data = pd.read_csv(file_path)
|
27 |
+
elif file_path.endswith(".json"):
|
28 |
+
data = json.load(open(file_path))
|
29 |
+
else:
|
30 |
+
data = open(file_path, "r").read()
|
31 |
+
else:
|
32 |
+
data = ""
|
33 |
+
|
34 |
+
prompt = f"""
|
35 |
+
Answer the question based on the provided context:
|
36 |
+
|
37 |
+
Question: {question}
|
38 |
+
|
39 |
+
Context: {data}
|
40 |
+
|
41 |
+
Answer:
|
42 |
+
"""
|
43 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
44 |
+
input_ids = inputs.input_ids.to(torch.device("cuda" if torch.cuda.is_available() else "cpu"))
|
45 |
+
attention_mask = inputs.attention_mask.to(torch.device("cuda" if torch.cuda.is_available() else "cpu"))
|
46 |
+
output = model.generate(input_ids=input_ids, attention_mask=attention_mask)
|
47 |
+
answer = tokenizer.decode(output[0], skip_special_tokens=True)
|
48 |
+
return answer
|
49 |
+
|
50 |
+
def main():
|
51 |
+
question = input("Enter your question: ")
|
52 |
+
file_path = input("Enter the file path (optional): ")
|
53 |
+
answer = generate_answer(question, file_path)
|
54 |
+
print(f"Answer: {answer}")
|
55 |
+
|
56 |
+
if __name__ == "__main__":
|
57 |
+
main()
|