nikunjcepatel commited on
Commit
4cb67db
·
verified ·
1 Parent(s): e731b38

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -74
app.py CHANGED
@@ -21,93 +21,87 @@ MODEL_OPTIONS = [
21
  "liquid/lfm-40b:free"
22
  ]
23
 
24
- # History storage
25
  history = []
26
 
27
- def generate_comparisons_with_history(input_text, selected_models, history_state):
28
  global history
29
- results = {}
30
- for model in selected_models:
31
- response = requests.post(
32
- url="https://openrouter.ai/api/v1/chat/completions",
33
- headers={
34
- "Authorization": f"Bearer {API_KEY}",
35
- "Content-Type": "application/json"
36
- },
37
- data=json.dumps({
38
- "model": model, # Use the current model
39
- "messages": [{"role": "user", "content": input_text}],
40
- "top_p": 1,
41
- "temperature": 1,
42
- "frequency_penalty": 0,
43
- "presence_penalty": 0,
44
- "repetition_penalty": 1,
45
- "top_k": 0,
46
- })
47
- )
48
-
49
- # Parse the response
50
- if response.status_code == 200:
51
- try:
52
- response_json = response.json()
53
- results[model] = response_json.get("choices", [{}])[0].get("message", {}).get("content", "No content returned.")
54
- except json.JSONDecodeError:
55
- results[model] = "Error: Unable to parse response."
56
- else:
57
- results[model] = f"Error: {response.status_code}, {response.text}"
58
 
59
- # Add input and results to history
 
 
 
 
 
 
 
 
 
 
 
60
  history_entry = {
61
  "input": input_text,
62
- "selected_models": selected_models,
63
- "outputs": results
64
  }
65
  history.append(history_entry)
66
 
67
  # Update the history state
68
  history_state = history
69
 
70
- return results, history_state
71
-
72
- def clear_history():
73
- global history
74
- history = []
75
- return history
76
-
77
- # Create Gradio interface with multiple model selection and history
78
- with gr.Blocks() as demo:
79
- input_text = gr.Textbox(lines=2, label="Input Text", placeholder="Enter your query here")
80
- selected_models = gr.CheckboxGroup(choices=MODEL_OPTIONS, label="Select Models", value=[MODEL_OPTIONS[0]])
81
-
82
- # Define output components with scrollable containers
83
- output_comparisons = gr.JSON(label="Model Comparisons", elem_id="output-comparisons")
84
- output_history = gr.JSON(label="History", elem_id="output-history")
85
 
86
- clear_history_button = gr.Button("Clear History")
87
 
88
- # Add a button to clear the history
89
- clear_history_button.click(clear_history, outputs=output_history)
90
-
91
- # Define the button to trigger generating comparisons
92
- generate_button = gr.Button("Generate Comparisons")
93
- generate_button.click(generate_comparisons_with_history, inputs=[input_text, selected_models, gr.State()], outputs=[output_comparisons, output_history])
 
 
 
 
 
 
 
 
94
 
95
- # Insert custom CSS using gr.HTML()
96
- gr.HTML("""
97
- <style>
98
- #output-comparisons {
99
- height: 300px;
100
- overflow: auto;
101
- border: 1px solid #ddd;
102
- padding: 10px;
103
- }
104
- #output-history {
105
- height: 300px;
106
- overflow: auto;
107
- border: 1px solid #ddd;
108
- padding: 10px;
109
- }
110
- </style>
111
- """)
112
 
113
- demo.launch()
 
21
  "liquid/lfm-40b:free"
22
  ]
23
 
24
+ # Initialize history
25
  history = []
26
 
27
+ def generate_text(input_text, selected_model, history_state):
28
  global history
29
+ response = requests.post(
30
+ url="https://openrouter.ai/api/v1/chat/completions",
31
+ headers={
32
+ "Authorization": f"Bearer {API_KEY}",
33
+ "Content-Type": "application/json"
34
+ },
35
+ data=json.dumps({
36
+ "model": selected_model, # Use selected model
37
+ "messages": [{"role": "user", "content": input_text}],
38
+ "top_p": 1,
39
+ "temperature": 1,
40
+ "frequency_penalty": 0,
41
+ "presence_penalty": 0,
42
+ "repetition_penalty": 1,
43
+ "top_k": 0,
44
+ })
45
+ )
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
+ # Handle errors
48
+ if response.status_code != 200:
49
+ return f"Error: {response.status_code}, {response.text}", history_state
50
+
51
+ # Parse and return the content of the response
52
+ try:
53
+ response_json = response.json()
54
+ result = response_json.get("choices", [{}])[0].get("message", {}).get("content", "No content returned.")
55
+ except json.JSONDecodeError:
56
+ result = "Error: Unable to parse response."
57
+
58
+ # Add the current interaction to the history
59
  history_entry = {
60
  "input": input_text,
61
+ "selected_model": selected_model,
62
+ "response": result
63
  }
64
  history.append(history_entry)
65
 
66
  # Update the history state
67
  history_state = history
68
 
69
+ # Prepare the formatted history string
70
+ formatted_history = "\n".join([f"Input: {entry['input']}\nModel: {entry['selected_model']}\nResponse: {entry['response']}\n" for entry in history])
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
+ return result, formatted_history
73
 
74
+ # Create Gradio interface with a dropdown for model selection
75
+ iface = gr.Interface(
76
+ fn=generate_text,
77
+ inputs=[
78
+ gr.Textbox(lines=2, label="Input Text", placeholder="Enter your query here"),
79
+ gr.Dropdown(choices=MODEL_OPTIONS, label="Select Model", value=MODEL_OPTIONS[0]),
80
+ gr.State()
81
+ ],
82
+ outputs=[
83
+ gr.Textbox(label="Response", placeholder="Response will be shown here"),
84
+ gr.Textbox(label="History", placeholder="Interaction history will be shown here", lines=10, interactive=False)
85
+ ],
86
+ title="Chat with OpenRouter Models"
87
+ )
88
 
89
+ # Insert custom CSS for scrollable sections
90
+ iface.add_component(gr.HTML("""
91
+ <style>
92
+ #output-comparisons {
93
+ height: 300px;
94
+ overflow: auto;
95
+ border: 1px solid #ddd;
96
+ padding: 10px;
97
+ }
98
+ #output-history {
99
+ height: 300px;
100
+ overflow: auto;
101
+ border: 1px solid #ddd;
102
+ padding: 10px;
103
+ }
104
+ </style>
105
+ """))
106
 
107
+ iface.launch()