Skip to content

Commit

Permalink
Add a separate interface for tuned model
Browse files Browse the repository at this point in the history
  • Loading branch information
chiffonng committed Dec 12, 2024
1 parent 29c5c65 commit a96861a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Otherwise, you can try the setup script:
bash setup.sh
```

It attempts to install with [uv](https://docs.astral.sh/uv/) (a fast, Rust-based Python package and project manager) using `.python-version` file and `pyproject.toml` file. This is the recommended way to manage the project, since its resolver is faster and more reliable than `pip`.
It attempts to install with [uv](https://docs.astral.sh/uv/) (a fast, Rust-based Python package and project manager) using `pyproject.toml` file. This is the recommended way to manage the project, since its dependency resolver is faster and more reliable than `pip`.

Otherwise, it falls back to `pip` installation.

Expand Down
70 changes: 70 additions & 0 deletions src/app/app2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""Gradio interface for generating mnemonics from instructions.
TODO: Combine this interface with the chatbot interface in app.py.
"""

import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer

model_name = "chiffonng/gemma2-9b-it-mnemonics"

# Load model and tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)


def generate_text(instruction: str) -> str:
"""Generate mnemonic from user input/instruction.
Args:
instruction (str): User instructions to generate mnemonic.
Returns:
str: Generated mnemonic text.
"""
inputs = tokenizer.encode(instruction, return_tensors="pt")
outputs = model.generate(inputs, max_length=256)
return tokenizer.decode(outputs[0], skip_special_tokens=True)


# Create simple Gradio interface
demo = gr.Interface(
fn=generate_text,
inputs=gr.Textbox(label="Instruction"),
outputs=gr.Textbox(label="Output"),
title="Mnemonic Generation",
description="Enter an instruction to generate mnemonic text.",
)


def chatbot_response(message: str, history: list) -> list:
"""Generates a response from the chatbot based on the input message and updates the conversation history.
Args:
message (str): The input message from the user.
history (list): The conversation history, a list of tuples where each tuple contains a user message and a chatbot response.
Returns:
list: The updated conversation history with the new message and response appended.
"""
inputs = tokenizer.encode(message, return_tensors="pt")
outputs = model.generate(inputs, max_length=100)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
history.append((message, response))
return history


# Create Gradio ChatInterface
chatbot = gr.ChatInterface(
fn=chatbot_response,
title="Mnemonic Generation Chatbot",
description="Chat with the model to generate mnemonics.",
retry_btn=True,
undo_btn=True,
clear_btn=True,
)


# Launch the interface
demo.launch()
# chatbot.launch()

0 comments on commit a96861a

Please sign in to comment.