File size: 2,154 Bytes
8cceff5
 
e3601f9
 
 
8cceff5
 
6d9dbbb
8cceff5
 
 
6d9dbbb
8cceff5
6b28df3
e3601f9
8cceff5
 
 
e3601f9
8cceff5
 
 
 
e3601f9
 
8cceff5
 
 
 
 
 
e3601f9
8cceff5
 
 
 
e3601f9
6b28df3
 
e3601f9
 
8cceff5
 
 
4156728
 
 
 
 
 
 
 
 
 
 
096db89
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
import torch
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer

# Load API token from environment variable (set in Hugging Face Spaces settings)
ACCESS_TOKEN = os.getenv("HUGGINGFACE_TOKEN")

# Ensure token is provided
if ACCESS_TOKEN is None:
    raise ValueError("❌ ERROR: Missing HUGGINGFACE_TOKEN! Set it in Hugging Face Spaces settings.")

# Model and tokenizer names
MODEL_NAME = "google/gemma-2b"

# Load tokenizer and model securely using the token
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, token=ACCESS_TOKEN)

model = AutoModelForCausalLM.from_pretrained(
    MODEL_NAME,
    token=ACCESS_TOKEN,
    torch_dtype=torch.float16,  # Use float16 to optimize memory
    device_map="auto"  # Automatically use GPU if available
)

# Define the chat function
def chat_with_gemma(prompt):
    inputs = tokenizer(prompt, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
    output = model.generate(**inputs, max_new_tokens=200)
    response = tokenizer.decode(output[0], skip_special_tokens=True)
    return response

# Create the Gradio interface
iface = gr.Interface(
    fn=chat_with_gemma,
    inputs=gr.Textbox(lines=5, placeholder="Enter your prompt..."),
    outputs="text",
    title="Gemma-2B Chatbot",
    description="A chatbot powered by Google's Gemma-2B model, running on Hugging Face Spaces."
)

# Launch the app
if __name__ == "__main__":
    iface.launch()

import fitz  # PyMuPDF

def extract_text_from_pdf(pdf_path):
    """Načte PDF a vrátí extrahovaný text."""
    doc = fitz.open(pdf_path)
    text = "\n".join([page.get_text("text") for page in doc])
    return text

text = extract_text_from_pdf("firemni_dokument.pdf")
print(text[:500])  # Ukázka prvních 500 znaků

documents = [
    extract_text_from_docx("firemni_dokument.docx"),
    "Toto je ručně přidaný textový dokument."
]

from sentence_transformers import SentenceTransformer

embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")

embeddings = embedder.encode(documents, convert_to_numpy=True)
print(embeddings.shape)  # (počet dokumentů, velikost vektoru)