File size: 1,585 Bytes
cddb3bf
182c77f
fadef10
cddb3bf
182c77f
 
 
 
 
 
 
 
 
cddb3bf
182c77f
 
 
cddb3bf
182c77f
cddb3bf
 
182c77f
 
cddb3bf
 
 
182c77f
 
 
 
 
 
 
 
 
 
 
 
 
 
cddb3bf
 
 
 
 
 
 
0e89de4
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
import os
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
import gradio as gr

# Load the model and tokenizer from Hugging Face
model_name = "Hastika/codellama-CodeLlama-34b-Instruct-hf"  # Adjust if necessary
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

# Create a pipeline for text generation
client = pipeline("text-generation", model=model, tokenizer=tokenizer)

# System prompt
system_prompt = {
    "role": "system",
    "content": "You are a useful assistant. You reply with efficient answers."
}

# Chat function
async def chat_groq(message, history):
    messages = [system_prompt]

    # Add conversation history to messages
    for msg in history:
        messages.append({"role": "user", "content": str(msg[0])})
        messages.append({"role": "assistant", "content": str(msg[1])})

    # Add the new user message
    messages.append({"role": "user", "content": str(message)})

    # Format the conversation history as a string for the model
    conversation = "\n".join([f"{msg['role']}: {msg['content']}" for msg in messages])

    # Generate response from the model
    response_content = client(conversation, max_length=1024, do_sample=True)[0]['generated_text']

    yield response_content

# Gradio interface
with gr.Blocks(theme=gr.themes.Monochrome(), fill_height=True) as demo:
    gr.ChatInterface(chat_groq,
                     clear_btn=None, 
                     undo_btn=None, 
                     retry_btn=None,
                    )

demo.queue()
demo.launch()