File size: 1,554 Bytes
815fa95
 
 
99640fb
493c487
 
11aeb76
815fa95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
493c487
 
815fa95
 
 
 
 
 
 
 
e4c78fe
 
815fa95
 
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
import os
#os.environ['HF_HOME'] = 'E:/huggingface_cache'

import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

# Load the model and tokenizer
tokenizer = AutoTokenizer.from_pretrained("Hawoly18/Adia_Llama3.1")
model = AutoModelForCausalLM.from_pretrained("Hawoly18/Adia_Llama3.1")

if tokenizer.pad_token is None:
  tokenizer.pad_token = tokenizer.eos_token

# Function to generate responses
def generate_response(question, max_length=512):
    input_text = f"Question: {question}\nRéponse:"
    input_ids = tokenizer.encode(input_text, return_tensors='pt', padding=True, truncation=True)
    attention_mask = input_ids != tokenizer.pad_token_id

    with torch.no_grad():
        output_ids = model.generate(
            input_ids,
            max_length=max_length,
            attention_mask=attention_mask,
            pad_token_id=tokenizer.eos_token_id,
            eos_token_id=tokenizer.eos_token_id,
            num_beams=5,  # Beam search for better quality
            no_repeat_ngram_size=2,  # Prevent n-gram repetition
            early_stopping=True
        )
    response = tokenizer.decode(output_ids[0], skip_special_tokens=True)
    response = response.replace(input_text, "").strip()
    return response

# Define the Gradio interface
interface = gr.Interface(
    fn=generate_response,
    inputs="text",
    outputs="text",
    title="Model Q&A Interface",
    description="Ask a question related to BSE and entrepreneurship!",
    
)

# Launch the interface
interface.launch(share=True)