File size: 3,179 Bytes
66ff3e9
 
8658e51
b944409
 
991d86a
2a864c8
66ff3e9
991d86a
 
 
2a23908
 
 
 
 
c1a4bd7
 
 
 
 
991d86a
 
33d5d58
305a1a4
 
 
4cb67db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4394f5b
4cb67db
 
33d5d58
4cb67db
 
 
 
33d5d58
4cb67db
33d5d58
 
2a03702
 
33d5d58
 
b3c9596
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ce6b5d
 
 
 
b3c9596
 
1ce6b5d
 
33d5d58
ec9dfba
b3c9596
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import gradio as gr
import requests
import json
import os

# Retrieve the OpenRouter API Key from the Space secrets
API_KEY = os.getenv("OpenRouter_API_KEY")

# Define available models for selection
MODEL_OPTIONS = [
    "openai/gpt-4o-mini-2024-07-18",
    "meta-llama/llama-3.1-405b-instruct",
    "nvidia/llama-3.1-nemotron-70b-instruct",
    "qwen/qwen-2.5-7b-instruct",
    "mistralai/mistral-large-2411",
    "microsoft/phi-3-medium-128k-instruct",
    "meta-llama/llama-3.1-405b-instruct:free",
    "nousresearch/hermes-3-llama-3.1-405b:free",
    "mistralai/mistral-7b-instruct:free",
    "microsoft/phi-3-medium-128k-instruct:free",
    "liquid/lfm-40b:free"
]

def generate_text(input_text, selected_model, history):
    if history is None:
        history = ""  # Initialize history if it's None
    
    response = requests.post(
        url="https://openrouter.ai/api/v1/chat/completions",
        headers={
            "Authorization": f"Bearer {API_KEY}",
            "Content-Type": "application/json"
        },
        data=json.dumps({
            "model": selected_model,  # Use selected model
            "messages": [{"role": "user", "content": input_text}],
            "top_p": 1,
            "temperature": 1,
            "frequency_penalty": 0,
            "presence_penalty": 0,
            "repetition_penalty": 1,
            "top_k": 0,
        })
    )
    
    # Handle errors
    if response.status_code != 200:
        return f"Error: {response.status_code}, {response.text}", history
    
    # Parse and return the content of the response
    try:
        response_json = response.json()
        generated_response = response_json.get("choices", [{}])[0].get("message", {}).get("content", "No content returned.")
    except json.JSONDecodeError:
        generated_response = "Error: Unable to parse response."

    # Append the new interaction to the history with model name
    history += f"Model: {selected_model}\nUser: {input_text}\nResponse: {generated_response}\n\n"
    
    return generated_response, history

# Define the Gradio layout using Blocks
with gr.Blocks() as iface:
    # Inject custom CSS using gr.HTML()
    gr.HTML("""
        <style>
            #output-response, #output-history {
                height: 300px;
                overflow: auto;
                border: 1px solid #ddd;
                padding: 10px;
            }
        </style>
    """)

    input_text = gr.Textbox(lines=2, label="Input Text", placeholder="Enter your query here")
    selected_model = gr.Dropdown(choices=MODEL_OPTIONS, label="Select Model", value=MODEL_OPTIONS[0])

    # Generate button positioned below the dropdown
    generate_button = gr.Button("Generate")

    output_response = gr.Textbox(label="Response", placeholder="Response will be shown here")
    output_history = gr.Textbox(label="History", placeholder="Interaction history will be shown here", lines=10, interactive=False)

    # Trigger the function when the user clicks the "Generate" button
    generate_button.click(generate_text, inputs=[input_text, selected_model, gr.State()], outputs=[output_response, output_history])

# Launch the interface
iface.launch()