SHIKARICHACHA commited on
Commit
5b8ac07
·
verified ·
1 Parent(s): 6001933

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -147
app.py DELETED
@@ -1,147 +0,0 @@
1
- import gradio as gr
2
- import os
3
- from openai import OpenAI
4
- import base64
5
- from PIL import Image
6
- import io
7
-
8
- # OpenRouter API key
9
- OPENROUTER_API_KEY = "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"
10
-
11
- # Available models
12
- MODELS = {
13
- "Mistral Small": "mistralai/mistral-small-3.2-24b-instruct:free",
14
- "Kimi Vision": "moonshotai/kimi-vl-a3b-thinking:free",
15
- "Gemini Pro": "google/gemini-2.5-pro-exp-03-25",
16
- "Qwen VL": "qwen/qwen2.5-vl-32b-instruct:free",
17
- "Mistral 3.1": "mistralai/mistral-small-3.1-24b-instruct:free",
18
- "Gemma": "google/gemma-3-4b-it:free",
19
- "Llama 3.2 Vision": "meta-llama/llama-3.2-11b-vision-instruct:free",
20
- }
21
-
22
- def image_to_base64(image):
23
- """Convert an image to base64 encoding"""
24
- # If image is a file path
25
- if isinstance(image, str):
26
- with open(image, "rb") as img_file:
27
- return base64.b64encode(img_file.read()).decode()
28
-
29
- # If image is already a PIL Image
30
- buffered = io.BytesIO()
31
- image.save(buffered, format="JPEG")
32
- return base64.b64encode(buffered.getvalue()).decode()
33
-
34
- def analyze_image(image, prompt, model_name):
35
- """Analyze an image using the selected OpenRouter model"""
36
- try:
37
- # Initialize OpenAI client with OpenRouter base URL
38
- client = OpenAI(
39
- base_url="https://openrouter.ai/api/v1",
40
- api_key=OPENROUTER_API_KEY,
41
- )
42
-
43
- # Convert image to base64
44
- img_base64 = image_to_base64(image)
45
-
46
- # Create the completion request
47
- completion = client.chat.completions.create(
48
- extra_headers={
49
- "HTTP-Referer": "https://gradio-openrouter-interface.com",
50
- "X-Title": "Gradio OpenRouter Interface",
51
- },
52
- model=MODELS[model_name],
53
- messages=[
54
- {
55
- "role": "user",
56
- "content": [
57
- {
58
- "type": "text",
59
- "text": prompt
60
- },
61
- {
62
- "type": "image_url",
63
- "image_url": {
64
- "url": f"data:image/jpeg;base64,{img_base64}"
65
- }
66
- }
67
- ]
68
- }
69
- ]
70
- )
71
-
72
- # Return the model's response
73
- return completion.choices[0].message.content
74
-
75
- except Exception as e:
76
- return f"Error: {str(e)}"
77
-
78
- # Create the Gradio interface
79
- with gr.Blocks(title="OpenRouter AI Vision Interface", css="style.css", theme=gr.themes.Soft()) as demo:
80
- gr.Markdown(
81
- """
82
- # 🔍 OpenRouter AI Vision Interface
83
-
84
- Upload an image and ask a question about it. The AI will analyze the image and respond.
85
-
86
- *Powered by OpenRouter API with multiple vision-language models*
87
- """
88
- )
89
-
90
- with gr.Row():
91
- with gr.Column():
92
- # Input components with custom styling
93
- with gr.Box(elem_classes=["input-container"]):
94
- image_input = gr.Image(type="pil", label="Upload Image", elem_classes=["image-upload-container"])
95
- prompt_input = gr.Textbox(label="Your Question", placeholder="What is in this image?", value="What is in this image?")
96
- model_dropdown = gr.Dropdown(
97
- choices=list(MODELS.keys()),
98
- value="Mistral Small",
99
- label="Select AI Model",
100
- info="Choose from different vision-language models"
101
- )
102
- submit_button = gr.Button("Analyze Image", variant="primary")
103
-
104
- with gr.Column():
105
- # Output component with custom styling
106
- with gr.Box(elem_classes=["output-container"]):
107
- output_text = gr.Textbox(label="AI Response", lines=12)
108
-
109
- gr.Markdown(
110
- """
111
- ### Available Models
112
- - **Mistral Small**: Powerful vision-language model from Mistral AI
113
- - **Kimi Vision**: Specialized vision model from Moonshot AI
114
- - **Gemini Pro**: Google's advanced multimodal model
115
- - **Qwen VL**: Alibaba's vision-language model
116
- - **Mistral 3.1**: Earlier version of Mistral's vision model
117
- - **Gemma**: Google's lightweight vision model
118
- - **Llama 3.2 Vision**: Meta's vision-enabled large language model
119
- """
120
- )
121
-
122
- # Set up the submit action
123
- submit_button.click(
124
- fn=analyze_image,
125
- inputs=[image_input, prompt_input, model_dropdown],
126
- outputs=output_text
127
- )
128
-
129
- # Add example
130
- gr.Examples(
131
- examples=[
132
- ["examples/nature.jpg", "What is in this image?", "Mistral Small"],
133
- ["examples/nature.jpg", "Describe this scene in detail", "Kimi Vision"],
134
- ],
135
- inputs=[image_input, prompt_input, model_dropdown],
136
- )
137
-
138
- # Create examples directory if it doesn't exist
139
- os.makedirs("examples", exist_ok=True)
140
-
141
- # For Hugging Face Spaces compatibility
142
- if __name__ == "__main__":
143
- # Launch the interface
144
- demo.launch(share=True)
145
- else:
146
- # For Hugging Face Spaces, we need to expose the app
147
- app = demo.launch(share=False, show_api=False)