Download src/streamlit_app.py from benxa34/r1: direct link, hf CLI and curl.
- Browser
- Download file 2.32 kB
-
https://huggingface.co/spaces/benxa34/r1/resolve/main/src/streamlit_app.py
- Command line
-
hf download hf://spaces/benxa34/r1/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/benxa34/r1/resolve/main/src/streamlit_app.py
2.32 kB
| import requests | |
| import json | |
| def extract_text_from_image(image_path, api_key, model, task_type, max_tokens, temperature, top_p, repetition_penalty, pages=None): | |
| url = "https://api.opentyphoon.ai/v1/ocr" | |
| with open(image_path, 'rb') as file: | |
| files = {'file': file} | |
| data = { | |
| 'model': model, | |
| 'task_type': task_type, | |
| 'max_tokens': str(max_tokens), | |
| 'temperature': str(temperature), | |
| 'top_p': str(top_p), | |
| 'repetition_penalty': str(repetition_penalty) | |
| } | |
| if pages: | |
| data['pages'] = json.dumps(pages) | |
| headers = { | |
| 'Authorization': f'Bearer {api_key}' | |
| } | |
| response = requests.post(url, files=files, data=data, headers=headers) | |
| if response.status_code == 200: | |
| result = response.json() | |
| # Extract text from successful results | |
| extracted_texts = [] | |
| for page_result in result.get('results', []): | |
| if page_result.get('success') and page_result.get('message'): | |
| content = page_result['message']['choices'][0]['message']['content'] | |
| try: | |
| # Try to parse as JSON if it's structured output | |
| parsed_content = json.loads(content) | |
| text = parsed_content.get('natural_text', content) | |
| except json.JSONDecodeError: | |
| text = content | |
| extracted_texts.append(text) | |
| elif not page_result.get('success'): | |
| print(f"Error processing {page_result.get('filename', 'unknown')}: {page_result.get('error', 'Unknown error')}") | |
| return '\n'.join(extracted_texts) | |
| else: | |
| print(f"Error: {response.status_code}") | |
| print(response.text) | |
| return None | |
| # Usage | |
| api_key = "sk-SJLOZn2kGqDX2zuO9doBAwb1alfocOLYQSM9SkiU2HW3KpQC" | |
| image_path = "path/to/your/image.jpg" # or path/to/your/document.pdf | |
| model = "typhoon-ocr" | |
| task_type = "default" | |
| max_tokens = 16384 | |
| temperature = 0.1 | |
| top_p = 0.6 | |
| repetition_penalty = 1.2 | |
| pages = None | |
| extracted_text = extract_text_from_image(image_path, api_key, model, task_type, max_tokens, temperature, top_p, repetition_penalty, pages) | |
| print(extracted_text) |