Spaces:
Runtime error
Runtime error
AliEssa555
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
|
5 |
+
# Load your model and tokenizer
|
6 |
+
from peft import PeftModel, PeftConfig
|
7 |
+
from transformers import AutoModelForCausalLM
|
8 |
+
|
9 |
+
config = PeftConfig.from_pretrained("AliEssa555/podcast_model_ft")
|
10 |
+
base_model = AutoModelForCausalLM.from_pretrained("TheBloke/Mistral-7B-Instruct-v0.2-GPTQ")
|
11 |
+
model_name = PeftModel.from_pretrained(base_model, "AliEssa555/podcast_model_ft")
|
12 |
+
|
13 |
+
#model_name = "path_to_your_fine_tuned_model" # Use the local path or the Hugging Face model hub ID if published
|
14 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16)
|
15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
16 |
+
|
17 |
+
if torch.cuda.is_available():
|
18 |
+
model = model.to("cuda")
|
19 |
+
|
20 |
+
# Generate a response based on user input
|
21 |
+
def generate_response(user_input):
|
22 |
+
# Format the input as an instructional prompt
|
23 |
+
prompt = f"[INST] User: {user_input} [/INST] Assistant:"
|
24 |
+
|
25 |
+
# Tokenize input and generate response
|
26 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
|
27 |
+
output_tokens = model.generate(inputs["input_ids"], max_length=512, temperature=0.7, top_p=0.9, do_sample=True)
|
28 |
+
|
29 |
+
# Decode and format the output
|
30 |
+
response = tokenizer.decode(output_tokens[0], skip_special_tokens=True)
|
31 |
+
return response.split("Assistant:")[-1].strip() # Remove "Assistant:" tag if present
|
32 |
+
|
33 |
+
# Define Gradio interface
|
34 |
+
with gr.Blocks() as demo:
|
35 |
+
gr.Markdown("## LLM Podcast Response Generator")
|
36 |
+
with gr.Row():
|
37 |
+
user_input = gr.Textbox(label="Enter your question related to the podcast:", placeholder="Type your question here...")
|
38 |
+
with gr.Row():
|
39 |
+
response_output = gr.Textbox(label="Model's Response")
|
40 |
+
submit_button = gr.Button("Generate Response")
|
41 |
+
|
42 |
+
# Connect button to the function
|
43 |
+
submit_button.click(fn=generate_response, inputs=user_input, outputs=response_output)
|
44 |
+
|
45 |
+
# Launch the Gradio app
|
46 |
+
demo.launch()
|