Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
@@ -1,55 +1,47 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
-
from typing import List, Tuple
|
4 |
import torch
|
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 |
-
outputs = model.generate(
|
33 |
-
**inputs,
|
34 |
-
max_length=max_tokens,
|
35 |
-
temperature=temperature,
|
36 |
-
top_p=top_p,
|
37 |
-
do_sample=True,
|
38 |
-
)
|
39 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True).split("Assistant:")[-1].strip()
|
40 |
return response
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
],
|
51 |
-
title="Chatbot Interface"
|
52 |
)
|
53 |
|
54 |
-
|
55 |
-
|
|
|
1 |
+
import os
|
2 |
+
#os.environ['HF_HOME'] = 'E:/huggingface_cache'
|
3 |
+
|
4 |
import gradio as gr
|
5 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
6 |
import torch
|
7 |
|
8 |
+
# Load the model and tokenizer
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained("Hawoly18/Adia_Llama3.1")
|
10 |
+
model = AutoModelForCausalLM.from_pretrained("Hawoly18/Adia_Llama3.1")
|
11 |
+
|
12 |
+
if tokenizer.pad_token is None:
|
13 |
+
tokenizer.pad_token = tokenizer.eos_token
|
14 |
+
|
15 |
+
# Function to generate responses
|
16 |
+
def generate_response(question, max_length=512):
|
17 |
+
input_text = f"Question: {question}\nRéponse:"
|
18 |
+
input_ids = tokenizer.encode(input_text, return_tensors='pt', padding=True, truncation=True)
|
19 |
+
attention_mask = input_ids != tokenizer.pad_token_id
|
20 |
+
|
21 |
+
with torch.no_grad():
|
22 |
+
output_ids = model.generate(
|
23 |
+
input_ids,
|
24 |
+
max_length=max_length,
|
25 |
+
attention_mask=attention_mask,
|
26 |
+
pad_token_id=tokenizer.eos_token_id,
|
27 |
+
eos_token_id=tokenizer.eos_token_id,
|
28 |
+
num_beams=5, # Beam search for better quality
|
29 |
+
no_repeat_ngram_size=2, # Prevent n-gram repetition
|
30 |
+
early_stopping=True
|
31 |
+
)
|
32 |
+
response = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
33 |
+
response = response.replace(input_text, "").strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
return response
|
35 |
|
36 |
+
# Define the Gradio interface
|
37 |
+
interface = gr.Interface(
|
38 |
+
fn=generate_response,
|
39 |
+
inputs="text",
|
40 |
+
outputs="text",
|
41 |
+
title="Model Q&A Interface",
|
42 |
+
description="Ask a question related to BSE and entrepreneurship!",
|
43 |
+
|
|
|
|
|
44 |
)
|
45 |
|
46 |
+
# Launch the interface
|
47 |
+
interface.launch(share=True)
|